suttercain Posted April 6, 2007 Share Posted April 6, 2007 Good afternoon everyone, I built a 3 page mini-site to test myself on SESSIONS. 1st Page has a single form with a First name field. THe second page attempts to echo out the 'first' variable based on the input in the form. The 3rd does the sams as page 2, but tries to retain the form input from page 1. First Page - Basic HTML Form: <html> <body> <form action="form.php" method="post"> First: <input type="text" name="first"><br> <input type="submit" name="submit"> </form> </body> </html> Second Page - Trying to Echo the Variable with a link to Page 3: <?php session_start(); $post = $_POST['first']; $first = $_SESSION['first']; $_SESSION['first'] = $_POST['first']; $wham = $_SESSION['first']; echo '$first:' . $first . "<br>"; echo '$wham:' . $wham . "<br>"; echo '$post:' . $post . "<br>"; echo "<a href=session.php>Session</a>"; ?> Third Page - Same as the Second, Minus the link: <?php session_start(); $post = $_POST['first']; $first = $_SESSION['first']; $_SESSION['first'] = $_POST['first']; $wham = $_SESSION['first']; echo '$first:' . $first . "<br>"; echo '$wham:' . $wham . "<br>"; echo '$post:' . $post . "<br>"; ?> As you can see I am trying to echo the input from the from based on three different variables: 1) The form is loaded and I enter my name, Shannon, and hit submit. 2) The second page loads and I get the following result: $first: $wham:Shannon $post:Shannon Session 3) When I click the link titled Session, I get the following result: $first:Shannon $wham: $post: Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0 4) When I click the back button via my browser and go back to the second page I get: $first:Shannon $wham:Shannon $post:Shannon Session I am new to SESSIONS, this is the first time I have ever attempted to use them, so excuse my ignorance. 1) Why when I got to the second page the first time is the $first: blank? 2) Yet when I vist the thir page it is filled with the form input? 3) What is the correct set-up for a basic SESSION like the one I have here? I thank you in advance for your help and suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/45826-solved-first-attempt-at-sessions-quick-explanation/ Share on other sites More sharing options...
KrisNz Posted April 6, 2007 Share Posted April 6, 2007 1. You've set $_SESSION['first'] after you set the variable $first, so you're trying to set $first to something that doesn't exist yet. 2. The session exists now, you set it on line 4 of page 2. Note that on page three, you havent POSTed anything to it, you click a link to get to that page so the $_POST array will be empty. 3. Always create things before trying to use them. Quote Link to comment https://forums.phpfreaks.com/topic/45826-solved-first-attempt-at-sessions-quick-explanation/#findComment-222655 Share on other sites More sharing options...
suttercain Posted April 6, 2007 Author Share Posted April 6, 2007 Cool, I understand now and got it working. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/45826-solved-first-attempt-at-sessions-quick-explanation/#findComment-222661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.