PHP sessions

Discussion in 'PHP' started by coder 525, Jul 9, 2008.

  1. #1
    Hi
    I want to use sessions.

    first page is login.php--- form for login
    Second page logincheck.php---- I am storing user id as session variable. I can read the session variable in this page.
    But when I redirect to another page, the session variable is lost or I can not display it...
    Any help...
     
    coder 525, Jul 9, 2008 IP
  2. zerofill

    zerofill Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #2
    need to start the session on each of those pages..

    if (!isset($_SESSION)) {
    session_start();
    }

    If the session isn't started that will do it...
    Then you can access it like $_SESSION['foo'];
     
    zerofill, Jul 9, 2008 IP
  3. coder 525

    coder 525 Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi
    I am sending the code. Nothing is displayed on the screen...

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php
    if (!isset($_SESSION)) {
    session_start();
    }
    ?>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <? php echo $_SESSION['userid']; ?>
    </body>
    </html>
     
    coder 525, Jul 9, 2008 IP
  4. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    session_id($ID); // pass the id again (not the user id)
    session_start();
    
    PHP:
    That should do that work
     
    Mozzart, Jul 9, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    If you are using cookie-based sessions (which you probably are), you must call session_start() before anything is outputted to the browser.

    Anyway, PHP and HTML should stay separated for readability, only basic loops and echos should be in the main body of the HTML itself.

    Dan
     
    Danltn, Jul 10, 2008 IP