Hi.
I have two forms with the following names and properties:
1. register.php --- ----- action="register-exec.php" --- method="POST"
2. register-exec.php --- action="register-exec.php" --- method="POST"


After submitting the register.php, I used session in register-exec.php to store the value of the register.php for later use as follow:

session_start();
$_SESSION['bookTitle'] = $_POST['txtBookTitle'];
$_SESSION['bookInfo'] = $_POST['txtBookInfo'];

Then after submitting the register-exec.php, I wanted to use those session variables, as follow:

if( isset($_POST['register-exec']) ){
     echo $bookTitle;
     echo $_SESSION['bookTitle'];
}

None of them work?
what is wrong ???

Dani AI

Generated

Brief summary: set values coming from the first form and expected to read them after the second submit, but the values did not appear and a hidden-field workaround was used. and pointed toward session initialization and inspecting session state. The underlying causes are almost always one of a few environmental or flow problems rather than a bug in PHP itself.

Things to check (quick checklist):

  • Confirm the PHP session mechanism is actually initialized and available on every request that sets or reads data. If session initialization is skipped or happens after output, values may not persist.
  • Verify the browser is returning the session identifier (check the PHPSESSID cookie in developer tools) so PHP can associate requests with stored session data.
  • Dump or log the server-side session array on each relevant request to confirm whether values are present server-side, and check server error logs for session-related warnings.
  • Be careful relying only on a submit-button name to detect a form submission; pressing Enter in a field may not send that named control. Detect by request method and expected form fields instead.
  • If code redirects, regenerates, or destroys the session between requests the data can be lost; ensure the flow writes session data and then uses the same session on the next request.
  • Check server settings (session.save_path permissions, garbage-collection timing) if sessions disappear intermittently.

Notes on approach: hidden inputs work for simple, public values but expose data to the client and can be tampered with. For sensitive or larger data prefer server-side sessions and use the POST-Redirect-GET pattern to avoid duplicate-post issues.

Official reference: PHP sessions manual

Recommended Answers

All 6 Replies

I hope you have session_start in all the required pages. If so, you can try to print out the session variables first, to see if it really has the values.
Try print_r($_SESSION); in all the pages and check if the session variables are really storing any values.

I stored the session in the register-exec.php, and in the same page I would like use those session variables, but when I use the echo and print_r, nothing display, I before Posting it is OK, but after that it is working.

Can you post your code ?

k..
I think u hav to use the seesion registrstion in your first page means in register.php .Then you can retrieve those session variables in the page where ever u want..


Thanks.

You have to use this code in register.php
Try this ...

session_start();
$_SESSION['bookTitle'] = $_POST['txtBookTitle'];
$_SESSION['bookInfo'] = $_POST['txtBookInfo'];

Hi.
Thanks for replying. Finally I used the <Input Hidden /> element of the form. and it worked well.

I will use the posted comments too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.