new session always created on session_start()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smudge
    New Member
    • Jun 2007
    • 3

    new session always created on session_start()

    Hi everybody,

    I am moving a php site to new hosting, and what was working fine on the old hosting is now broken on new hosting. The problem is that session_start is always creating a new session, even though one exists. I can see that the cookie is created just fine locally on login, and the matching session file is created on the server. But once I go to a new page, my checklogin() calls session_start which returns a new empty session, and I am automatically logged out.

    Here is how I create the session during login:
    [PHP]if ($rs->RecordCount( ) == 1) { // Found a partner
    session_start() ;
    $_SESSION['real-usr'] = $usr;
    }[/PHP]

    Here is how I check that the user is logged in:

    [PHP]function checkLogin() {
    session_start() ;
    if (empty($_SESSIO N['real-usr'])) {
    echo "Session expired. Please login";
    include("login. php");
    die();
    }
    }[/PHP]

    Note that it isn't shared hosting, so there shouldn't be a problem with other processes deleting the temporary session files. (Plus I can see that the temp files are still there.)

    I am using PHP 5.2.2. Here is an exerpt from phpinfo()

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

    Directive Local Value Master Value
    session.auto_st art Off Off
    session.bug_com pat_42 On On
    session.bug_com pat_warn On On
    session.cache_e xpire 180 180
    session.cache_l imiter nocache nocache
    session.cookie_ domain .semoptimizer.c om .semoptimizer.c om
    session.cookie_ httponly Off Off
    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_divi sor 1000 1000
    session.gc_maxl ifetime 1440 1440
    session.gc_prob ability 1 1
    session.hash_bi ts_per_characte r 5 5
    session.hash_fu nction 0 0
    session.name PHPSESSID PHPSESSID
    session.referer _check no value no value
    session.save_ha ndler files files
    session.save_pa th C:\Temp C:\Temp
    session.seriali ze_handler php php
    session.use_coo kies On On
    session.use_onl y_cookies Off Off
    session.use_tra ns_sid 0 0

    If anybody can give me a hand, it would be much appreciated. I have been doing google searches all day and investigating different options, but haven't figured anything out.

    Thanks,

    jessica
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    i'm not 100% sure but maybe the problem is that your calling session_start() inside your functions.

    session_start() should be the first line (after the <? tag), before any other code is used, and should only be called once per php file.

    does that work?

    Comment

    • smudge
      New Member
      • Jun 2007
      • 3

      #3
      Originally posted by epots9
      i'm not 100% sure but maybe the problem is that your calling session_start() inside your functions.

      session_start() should be the first line (after the <? tag), before any other code is used, and should only be called once per php file.

      does that work?
      Nope, that didn't fix it. It's still doing the same thing.

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        This may be an issue with the session cookie options. Try creating a cookie yourself and see if you get the same Host, Path, and Domain information in that cookie as you do in your Session cookie.

        Comment

        • smudge
          New Member
          • Jun 2007
          • 3

          #5
          Originally posted by Motoma
          This may be an issue with the session cookie options. Try creating a cookie yourself and see if you get the same Host, Path, and Domain information in that cookie as you do in your Session cookie.
          Thanks! That did the trick. The domain was missing a www and the path was / instead of \.

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by smudge
            Thanks! That did the trick. The domain was missing a www and the path was / instead of \.
            Glad you got everything working, and thanks for posting the solution so others may learn too.
            Come back to the site any time you have a problem.

            Comment

            • jx2
              New Member
              • Feb 2007
              • 228

              #7
              try that :
              [PHP]if(!(session_id ()))session_sta rt();[/PHP]

              why do you use it many times?
              i woud use it once at first line
              let me know if it works...
              jx2

              Comment

              Working...