I am having issues displaying $_SESSION variables on later pages in my site. Here is a quick synopsis of what I'm trying to accomplish: The user inputs their information into a form on page1. The input fields are 'first' and 'last'. That data is sent to a table in a MySQL database (this part works just fine). Now, I want the data 'first' and 'last' to be displayed on the next page (page2)- like "Welcome 'first' 'last'!" Here is a snippet of the code I'm working with: Page1: <?php $_SESSION['userFirstName'] = $first; $_SESSION['userLastName'] = $last; ?> PHP: Page2: <?php session_start (); <?php echo $_SESSION['userFirstName']; ?> <? echo $_SESSION['userLastName']; ?> ?> PHP: What am I missing? HELP!!!
Simple fix Page1: <?php session_start(); // This must begin ALL affected files! $_SESSION['userFirstName'] = $first; $_SESSION['userLastName'] = $last; ?> PHP: If this is going to be a big script, instead of defining session_start(); (which must always go before EVERYTHING - except comments [// or /* comment */] and <?php of course) in every file, create a file such as init.php and just make sure session_start(); is the first thing in it. Then make sure it is the first document included in all your scripts! Regards, Dennis M.
Place session_start(); on the first one aswell... it's a common one, but any page using sessions, must have the session_start() in the code
Thanks guys... I actually had a include ('config.php'); on the first line of each of those pages, which includes a session_start (); at the very beginning, but was really only putting in the 'juicy' code. Any other suggestions, knowing that session_start was already there?
Refresh your browser. I've found renaming the config.php sometimes works (For whatever reason I sometimes get processing errors when I include a "config.php" file name ) but doubt that's the case. This setup should work: config.php <?php session_start(); // Config vars ?> PHP: Page1: <?php include_once("config.php"); $_SESSION['blah'] = "w00t"; print "Next page: <a href=\"page2.php\">Here</a>"; ?> PHP: Page2: <?php include_once("config.php"); print $_SESSION['blah']." this should work... o_O"; session_close(); // Kill session... ?> PHP: That setup should have no problems Regards, Dennis M.
Thanks Dennis, should just have one last question regarding the code below: $_SESSION['blah'] = "w00t"; This all should work fine assuming in my form "woot" is the input field name that I am referencing, correct?
It worked fine when the $_SESSION is defined on the page, but I am trying to define it as data submitted to a form. What's the exact way to set $_SESSION as a $_POST variable? Here's how I have it: <?php session_start(); // Page1.php (coming from the html form) // Grab the POST data that was inputted $first = $_POST['txtUserFirstName']; $last = $_POST['txtUserLastName']; // Once the POST data has been put into variables assign them to the sessions $_SESSION['fName'] = $first; $_SESSION['lName'] = $last; print "Next page: <a href=\"page2.php\">Here</a>"; //direct user to next page ?> <?php session_start(); // Page2.php (coming from page1.php) print "welcome ".$_SESSION['fName']." how are you today?"; ?> PHP: The problem is, page2 displays "welcome how are you today?" It completely ignores the session variable. Suggestions?
Your code looks perfect to me. You should test to see if $first and $last are storing the right information on Page1; maybe your form input is under the wrong name. Hm...
I think I found your problem. I copied your code exactly (making my own form, however, this step is not 100% crucial) and found you had a whitespace before the session_start(); on page2.php! session_start(); must be absolutely first (unless otherwise separated by comments) whitespaces are considered characters in this regard. So here is what I've come up with: form.html <form name="w00t" method="post" action="page1.php"> <p>First Name: <input type="text" name="txtUserFirstName" /></p> <p>Last Name: <input type="text" name="txtUserLastName" /></p> <p><input type="submit" value="Make Session!" /> </form> HTML: page1.php <?php session_start(); // Page1.php (coming from the html form) // Grab the POST data that was inputted $first = $_POST['txtUserFirstName']; $last = $_POST['txtUserLastName']; // Once the POST data has been put into variables assign them to the sessions $_SESSION['fName'] = $first; $_SESSION['lName'] = $last; print "Next page: <a href=\"page2.php\">Here</a>"; //direct user to next page ?> PHP: page2.php <?php session_start(); // Page2.php (coming from page1.php) print "welcome ".$_SESSION['fName']." how are you today?"; ?> PHP: Regards, Dennis M.
This is what you need to change - you forgot to start a session ! Use the code below to get it working .. <?php session_start(); // Without starting a new session, you are not allowed to use it's objects .. $_SESSION['userFirstName'] = $first; $_SESSION['userLastName'] = $last; ?> PHP: