Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2006
    Posts
    1

    Unanswered: Lost session (variables?) issues

    I am attempting to incorporate user authentication into a site, however, I am having some significant issues with ensuring that session variables get stored. I have read a number of posts on the subject and have done the best I can, but I am at a loss as to why the variables are consistently lost.


    php.ini settings:

    PHP Version 5.0.5




    Session Support enabled
    Registered save handlers files user sqlite
    Registered serializer handlers php php_binary wddx

    Directive Local Value Master Value
    session.auto_start Off Off
    session.bug_compat_42 On On
    session.bug_compat_warn On On
    session.cache_expire 20 20
    session.cache_limiter nocache nocache
    session.cookie_domain www.mydomain.com
    session.cookie_lifetime 0 0
    session.cookie_path / /
    session.cookie_secure Off Off
    session.entropy_file no value no value
    session.entropy_length 0 0
    session.gc_divisor 5 5
    session.gc_maxlifetime 1200 1200
    session.gc_probability 1 1
    session.hash_bits_per_character 4 4
    session.hash_function 0 0
    session.name PHPSESSID PHPSESSID
    session.referer_check no value no value
    session.save_handler files files
    session.save_path C:\localdir C:\PHP\localdir
    session.serialize_handler php php
    session.use_cookies On On
    session.use_only_cookies On On
    session.use_trans_sid 0 0





    webpages:

    1) Login page, sends user input post method to a php script

    2) Php login script:

    <?
    session_start();

    //perform username and password logic to determine if they are an
    //acceptable match


    if ( //both username and password are correct )
    {
    session_register("the user name");
    $_SESSION["the user name"] = //the username;
    session_write_close();

    if ( //some other condition )
    {
    header("go to a page");
    exit;
    }
    else
    {
    header( "go to the index page" );
    exit;
    }
    }
    else
    {
    session_destroy();
    Header( "diplay error");
    exit;
    }
    ?>








    3) Then there is an index page, which in many cases is unable to access the session variable:



    <?
    if (!isset ($_SESSION))
    {
    echo"session not started, starting....<br><br>";
    session_start();
    }

    if (!session_is_registered('the user name'))
    {
    echo "session variable not set";
    exit;
    }

    if ($the user name == "")
    {
    echo "session variable is empty";
    exit;
    }
    else
    {
    echo "session variable: $the user name";
    exit;
    }
    ?>




    2 times out of 10, it works, the rest of the time it outputs the session not started message (obviously), and then
    the session variable not set message.

    Help?

  2. #2
    Join Date
    Jul 2004
    Location
    Dundee, Scotland
    Posts
    107
    on every page the first thing you have is "session_start();" to keep the session alive, not to start it if it has not been started already.

    I always use:
    if (!session_is_registered('user_name')) {
    session_register("user_name");
    }

    also, to debug, instead of using "session_destroy();", you might use:
    unset($GLOBALS['HTTP_SESSION_VARS']['user_name']);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •