This is a discussion on Sessions not getting updated within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I posted on this matter earlier with the subject line "Trouble with sessions," but someone else started ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I posted on this matter earlier with the subject line "Trouble with sessions," but someone else started a different thread with the same subject line that arrived before my post, so I'm changing my subject line in order to avoid confusion. I was asked to provide a code example for analysis, so here it is, along with a a description of what the problem is. I'm working with sessions and the values for HTML text fields (input="text") aren't getting updated after the first time I submit the form. If I shut down my browser and start anew, I can write to the session just once, as before. On the other hand, radio buttons -- used with the Sticky Multivalued Parameters script on pages 170-171 of O'Reilly's "Programming PHP" -- are updated every time. Here's an example of my code. Many thanks to anyone who can make sense of what's going on here. Best wishes, Mountain Man <html> <head> <title>First Form Sessions</title> </head> <body> <form action="form_processor.php4" method="POST"> First Name: <input type="text" name="firstName" value="<?php echo $firstName ?>" /><br /> <input type="submit" /> </form> </body> </html> // Next, the form processing script which is on another page. // In my more complex script of actual use the form is only // included if it doesn't pass validation. <?php session_start(); session_register("firstName"); ?> <html> <head> <title>Form Processor Sessions</title> </head> <body> <?php echo "$firstName<br />"; include 'first_form.php4'; ?> </body> </html> |
|
|||
|
Mountain Man wrote:
> I'm working with sessions and the values for HTML text fields > (input="text") aren't getting updated after the first time I submit > the form. If I shut down my browser and start anew, I can write to the > session just once, as before. On the other hand, radio buttons -- used > with the Sticky Multivalued Parameters script on pages 170-171 of > O'Reilly's "Programming PHP" -- are updated every time. Here's an > example of my code. Many thanks to anyone who can make sense of what's > going on here. > Using php version >= 4.1? Then start using the $_SESSION superglobal: <? // Enable sessions session_start(); // Get the page URL $page = $_SERVER['PHP_SELF']; // Destroy the session var if (isset($_GET['unset'])) { if (isset($_SESSION['test'])) { unset($_SESSION['test']); header("Location: $page"); exit; } } // Update/create the session var if (isset($_SESSION['test'])) { $_SESSION['test']++; } else { $_SESSION['test'] = 1; } // Debug output print "test = {$_SESSION['test']}<br />"; print "<a href='$page?unset=1'>Destroy session</a>"; ?> JW |
|
|||
|
Hi mountain man:
(from) http://nl.php.net/manual/en/function.session-start.php --------------------------- session_start (PHP 4 ) session_start -- Initialize session data Description bool session_start ( void ) session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie. This function always returns TRUE. Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser. --------------------------- Read that nore carefully. Your code calls session_start halfway your script, AFTER output, so correct that first before anything else. Futhermore I see that you are echoing $firstName without initializing it. Did you put the (retarded) register globals on in your php.ini? Don't, it is bad. Modern php comes with registerglobals off by default. Better is this: $firstName = $_GET["firstName"] or $firstName = $_POST["firstName"] or even better: $firstName = (isset($_GET["firstName"]) ? $_GET["firstName"]: "unkown") Regards, Erwin Moller |