1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple PHP question - $_SESSION

Discussion in 'PHP & Perl' started by berzerker, Feb 21, 2009.

  1. berzerker

    berzerker Registered Member

    Joined:
    Jan 27, 2009
    Messages:
    76
    Likes Received:
    9
    hey everyone, I've got a pretty basic question about using $_SESSION in PHP.

    here goes: a user enters their name and info and page1, and I want their information to display as "welcome user" on page 2.

    It doesn't seem difficult at all, but when page 2 loads, the echo return nothing, they're blank.

    Page 1:
    PHP:
    <? session_start ();
    $_SESSION['userFirstName'] = $first;
    $_SESSION['userLastName'] = $last;
    $_SESSION['userAddress'] = $address;
    $_SESSION['userCity'] = $city;
    $_SESSION['userState'] = $state;

    include (
    'page2.php');
    header('Refresh: 0;url=hyperlink to page 2');
     
    ?>
    Page 2:
    PHP:
    <?php session_start ();
     echo
    $_SESSION['userFirstName']; 
     echo
    $_SESSION['userLastName'];  <br />
    echo
    $_SESSION['userAddress']; <br />
    echo
    $_SESSION['userCity']; 
     echo
    $_SESSION['userState']; ?>
    It seems like the easiest thing in the world, HELP!
     
  2. graham23s

    graham23s BANNED BANNED

    Joined:
    Jul 26, 2008
    Messages:
    154
    Likes Received:
    106
    Hi Matey,

    on page 1, where are you getting the $first, $last variables from? also you are incuding page 2 in page 1 instead of hyperlinking it, can you comment out the refresh :)

    Graham
     
  3. confined

    confined Regular Member

    Joined:
    Jan 4, 2009
    Messages:
    216
    Likes Received:
    91
    FIrst, make sure <?php is the VERY FIRST THING On the page.

    Second, there needs to be a space after echo. so it would look like this:

    PHP:
    <?php
    echo $_SESSION['userFirstName'];

    //etc
    ?>
    If that doesn't work, use session_write_close(); at the bottom of page 1.
     
  4. berzerker

    berzerker Registered Member

    Joined:
    Jan 27, 2009
    Messages:
    76
    Likes Received:
    9
    on page 1 the vars come from a form with fields 'first' and 'last' etc. The user is inputting their info into my form which submits to a MySQL database.

    Once the form is submitted it directs to page 1, where the $_SESSION information is defined.

    If I comment out the refresh and inactivate it, won't the page have nowhere to go?

    Your help is much appreciated :)
     
  5. graham23s

    graham23s BANNED BANNED

    Joined:
    Jul 26, 2008
    Messages:
    154
    Likes Received:
    106
    Hi Mate,

    if you do:

    PHP:
    <a href="page2.php">Click here to goto page 2</a>
    this is a lot better to navigate to the second page than refresh :) can you put <?php instead of <? (this is totally depending on how your server is set up)

    also can you echo out $first as a test just to make sure it contains a value :)

    Graham
     
  6. confined

    confined Regular Member

    Joined:
    Jan 4, 2009
    Messages:
    216
    Likes Received:
    91
    Actually, after looking at it more thoroughly... it won't work if you include page2.php because the session didn't write or close. You need to redirect or use a href like said. It won't work with include()
     
  7. berzerker

    berzerker Registered Member

    Joined:
    Jan 27, 2009
    Messages:
    76
    Likes Received:
    9
    OK, here's a sample of the code I have, see what you think...

    Assume 'first' and 'last' are input in a form on the very previous page.

    Page1:
    PHP:
    <?php
    session_start 
    ();
    $_SESSION['userFirstName'] = $first;
    $_SESSION['userLastName'] = $last;
    ?> 
    Page2:
    PHP:
    <?php session_start(); ?>
    <?php 
    echo $_SESSION['userFirstName']; ?><?php echo $_SESSION['userLastName']; ?>
    That seems like the most basic usage $_SESSION - shouldn't that work?

    @confined - Are you saying that I need to write or close the session page1?

    Thanks again
     
  8. graham23s

    graham23s BANNED BANNED

    Joined:
    Jul 26, 2008
    Messages:
    154
    Likes Received:
    106
    Hi Mate,

    "Assume 'first' and 'last' are input in a form on the very previous page."

    You need to catch the input on page 1 before you can assign it to a session like:

    PHP:
      <!-- The html form index.php -->
      <form action="page1.php" method="POST" />
      <input type="text" name="txtUserFirstName"  />
      <input type="text" name="txtUserLastName"  />
      <input type="submit" name="submit-info" value="Ok" />
      </form>

    <?php
     session_start
    (); // Must always be first :)
     // 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;

    ?>

    <?php
     session_start
    (); // Must always be first :)
     
     // Page2.php (coming from page1.php)
    print "welcome $first how are you today?";
    ?>
    hope that helps :)

    Graham
     
  9. lord

    lord Junior Member

    Joined:
    Jan 21, 2009
    Messages:
    176
    Likes Received:
    251
    CODE for page 1
    PHP:
    <? session_start ();
    $_SESSION['userFirstName'] = $first;
    $_SESSION['userLastName'] = $last;
    $_SESSION['userAddress'] = $address;
    $_SESSION['userCity'] = $city;
    $_SESSION['userState'] = $state;

    header('Location: http://www.example.com/page2.php');
     
    ?>
    CODE FOR PAGE 2

    PHP:
    <?php session_start ();
     echo 
    $_SESSION['userFirstName']; 
     echo 
    $_SESSION['userLastName'];
    echo 
    $_SESSION['userAddress'];
    echo 
    $_SESSION['userCity']; 
     echo 
    $_SESSION['userState']; ?>
    THIS WILL WORK