Simple PHP $_SESSION problem, help!

Discussion in 'PHP' started by bersude2, Feb 21, 2009.

  1. #1
    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!!!
     
    bersude2, Feb 21, 2009 IP
  2. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    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.
     
    Dennis M., Feb 21, 2009 IP
  3. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #3
    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
     
    Grit., Feb 21, 2009 IP
  4. bersude2

    bersude2 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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?
     
    bersude2, Feb 21, 2009 IP
  5. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    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 :confused:) 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 :confused:

    Regards,
    Dennis M.
     
    Dennis M., Feb 21, 2009 IP
  6. bersude2

    bersude2 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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?
     
    bersude2, Feb 21, 2009 IP
  7. mustafaneguib

    mustafaneguib Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7

    ya, just replace "w00t" with your variable name, and you are set to go.
     
    mustafaneguib, Feb 22, 2009 IP
  8. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Yeah, that's right. Any word on how that worked out for you?

    Regards,
    Dennis M.
     
    Dennis M., Feb 22, 2009 IP
  9. bersude2

    bersude2 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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?
     
    bersude2, Feb 22, 2009 IP
  10. NFreak

    NFreak Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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...
     
    NFreak, Feb 22, 2009 IP
  11. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #11
    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.
     
    Dennis M., Feb 22, 2009 IP
  12. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #12
    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:
     
    ActiveFrost, Feb 22, 2009 IP