SESSION variable gets cleared

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Per Johansson

    SESSION variable gets cleared

    I have two files login.php and loggedin.php. If I understand $_SESSION
    correctly, its values should stay in place when I go from login.php to
    loggedin.php, right? They won't, $_SESSION gets cleared in
    loggedin.php. Have I done anything wrong?

    Here are my test files:


    login.php

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>
    PHP: Log In
    </title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=ISO-8859-1" />
    </head>
    <body>
    <div id="pageheader" >
    <h1>
    Pot Head Pixie: PHP
    </h1>
    </div>
    <div id="main">
    <!-- short_open_tag = Off -->
    <?php
    echo '<p>Hello World</p>';
    echo $_SERVER['HTTP_USER_AGEN T'];
    echo str_replace('\\ ','/',getcwd());

    @session_destro y();
    @session_start( );
    unset($adminlog in);
    unset($adminpas s);
    unset($adminip) ;
    $_SESSION['adminlogin']=md5('adm');
    $_SESSION['adminpass']=md5('pass');
    unset($adminlog in);
    unset($adminpas s);
    unset($adminip) ;

    echo '<br />';
    echo '$_SESSION[\'adminlogin\']: ';
    echo $_SESSION['adminlogin'];
    echo '<br />';
    if (isset($_SESSIO N['adminlogin'])) {
    echo "adminlogin is set";
    } else {
    echo "adminlogin is not set";
    }
    ?>
    <form action="loggedi n.php" method="post">
    <p>
    <input type="text" value="user" />
    </p>
    <p>
    <input type="password" value="password " />
    </p>
    <p>
    <input type="submit" value="OK" />
    </p>
    </form>
    </div>
    </body>
    </html>




    loggedin.php


    <h1>
    PHP: Logged In
    </h1>
    </div>
    <div id="main">
    <!-- short_open_tag = Off -->
    <?php
    echo '<p>Hello World</p>';
    echo $_SERVER['HTTP_USER_AGEN T'];
    echo str_replace('\\ ','/',getcwd());


    echo '<br />';
    echo '$_SESSION[\'adminlogin\']: ';
    echo $_SESSION['adminlogin'];
    echo '<br />';

    if (isset($_SESSIO N['adminlogin'])) {
    echo "adminlogin is set";
    } else {
    echo "adminlogin is not set";
    }

    ?>
    <form action="loggedo ut.php" method="post">
    <p>
    <input type="submit" value="Logout" />
    </p>
    </form>
    </div>
    </body>
    </html>

    --
    Per Johansson
    Systems developer


  • TJ Talluto

    #2
    Re: SESSION variable gets cleared

    Per Johansson wrote:
    [color=blue]
    > session_destroy ();
    > session_start() ;[/color]

    You need these to appear every time a page (but not including all of its
    includes) is returned from a trip to your server.


    login.php
    session_destroy ();
    session_start() ;


    loggedin.php
    session_destroy ();
    session_start() ;
    include_once('j avascript_inclu des.js');
    include_once('b anner.htm');
    print "you are logged in";


    You can see in the second example, that you only need to start your session
    once, you do not also need to start it on the pages that are included from
    loggedin.php.

    --
    TJ Talluto
    torpedo51 at yahoo dot com

    Comment

    • Per Johansson

      #3
      Re: SESSION variable gets cleared

      On Mon, 1 Nov 2004 20:55:50 UTC, TJ Talluto <tj@getlostspam mers.com>
      wrote:
      [color=blue]
      > Per Johansson wrote:
      >[color=green]
      > > session_destroy ();
      > > session_start() ;[/color]
      >
      > You need these to appear every time a page (but not including all of its
      > includes) is returned from a trip to your server.
      >
      >
      > login.php
      > session_destroy ();
      > session_start() ;
      >
      >
      > loggedin.php
      > session_destroy ();
      > session_start() ;
      > include_once('j avascript_inclu des.js');
      > include_once('b anner.htm');
      > print "you are logged in";
      >
      >
      > You can see in the second example, that you only need to start your session
      > once, you do not also need to start it on the pages that are included from
      > loggedin.php.
      >[/color]

      OK, I added to loggedin.php these lines first in the script (I haven't
      included anything yet):
      session_destroy ();
      session_start() ;

      So now $_SESSION retains its values.


      This is the first day ever I have worked with PHP, so there are some
      novelties.

      --
      Per Johansson
      Systems developer


      Comment

      Working...