PHP Sessions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • captainmerton
    New Member
    • May 2007
    • 12

    PHP Sessions

    I cant get PHP sessions to work.

    I have a login page with this code:

    session_start() ;
    $_SESSION['userid'] = $userid;

    echo $_SESSION['userid'];

    When i access the page and enter my login details it runs through this piece of code and the echo statement prints out the userid as expected.

    However when i then click an href to go to another page on my site I have the following code:

    session_start() ;
    $userid=$_SESSI ON['userid'];
    echo $userid;

    The echo statement returns nothing. Am i using sessions correctly or is there something more I need to do?

    Cheers.
  • captainmerton
    New Member
    • May 2007
    • 12

    #2
    I am displaying the SID when i click between pages and the PHPSESSID numbers are different. Why would this happen?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Originally posted by captainmerton
      I am displaying the SID when i click between pages and the PHPSESSID numbers are different. Why would this happen?
      This can happen if your PHP session configuration is incorrect. On some systems you must set the 'session.save_p ath'. You also might want to set the 'session.use_co okies' to 1.

      Comment

      • captainmerton
        New Member
        • May 2007
        • 12

        #4
        My web provider doesnt really offer much guidance but they said i could create php.ini files so i did. i added one to my webspace and set 'session.save_p ath' and the cookies indiciator to 1. I am getting various error messages when i run the scripts and i am certain the problem is the 'session.save_p ath' setting. I am on a Linux server. The book i have says set this to the C drive on Windows but that would be for a microsoft server i assume. Can anyone give me some pointers to what i should be setting the 'session.save_p ath' to?

        Cheers.

        Comment

        • rug
          New Member
          • May 2007
          • 6

          #5
          Originally posted by captainmerton
          My web provider doesnt really offer much guidance but they said i could create php.ini files so i did. i added one to my webspace and set 'session.save_p ath' and the cookies indiciator to 1. I am getting various error messages when i run the scripts and i am certain the problem is the 'session.save_p ath' setting. I am on a Linux server. The book i have says set this to the C drive on Windows but that would be for a microsoft server i assume. Can anyone give me some pointers to what i should be setting the 'session.save_p ath' to?

          Cheers.
          Here's some info from http://au3.php.net/session that might help you:
          session.save_pa th string

          session.save_pa th defines the argument which is passed to the save handler. If you choose the default files handler, this is the path where the files are created. Defaults to /tmp. See also session_save_pa th().

          There is an optional N argument to this directive that determines the number of directory levels your session files will be spread around in. For example, setting to '5;/tmp' may end up creating a session file and location like /tmp/4/b/1/e/3/sess_4b1e384ad7 4619bd212e236e5 2a5a174If . In order to use N you must create all of these directories before use. A small shell script exists in ext/session to do this, it's called mod_files.sh. Also note that if N is used and greater than 0 then automatic garbage collection will not be performed, see a copy of php.ini for further information. Also, if you use N, be sure to surround session.save_pa th in "quotes" because the separator (;) is also used for comments in php.ini.
          Warning

          If you leave this set to a world-readable directory, such as /tmp (the default), other users on the server may be able to hijack sessions by getting the list of files in that directory.

          Hope this helps,
          rug

          Comment

          Working...