We help IT Professionals succeed at work.

PHP Session Variables not staying set on next page

Medium Priority
693 Views
Last Modified: 2008-01-29
Hi,

I have a PHP script that I use every year around this time.  Last year it worked fine, this year it doesn't.  I wondered if anyone could see what I'm missing...

I have a login script that users type their username/password in.  Once the info is verified via MySQL I move on to the next page using this code:

                  $userid = $row["ID"];
                              
                  //Set cookie and seesion info
                  setcookie("admin", "loggedin", time()+3600);
                  setcookie("userid", "$userid", time()+3600);
                  $_SESSION["userid"] = $userid;
                  //echo $_SESSION["userid"];
                  header("Location: Grid.php");
                  exit();

When I test it by printing the userid is it being set. (i.e. 20 will print on the screen).

When I move on to the next page, the $_Session variable is no longer set.  The first few lines of Grid.php are:

<?
      //Security
      session_start();
      echo "USER:".$_SESSION["userid"];

At this point the $_Session variable is blank and that's what prints on the screen (nothing).

Has something changed about Sessions or is there something I'm missing.  I use a web host so I don't have a lot of control over PHP.  Any help would be appreciated!
Comment
Watch Question

Top Expert 2007

Commented:
I don't see a session_start() on the page where you do the check:

 //Set cookie and seesion info
                  setcookie("admin", "loggedin", time()+3600);
                  setcookie("userid", "$userid", time()+3600);

That might be a reason why $_SESSION does not contain the info set on the check page.

Commented:
Yup, its always handy to have session_start() in your config.php which gets included on each page.

Author

Commented:
Thanks guys.  The session start is included in the beginning of the login.php page (1st code snip).  That was the first thing I checked, sorry I should've mentioned that.
Top Expert 2007

Commented:
Does the following script works on your server? The value outputed should increase with each pageload.
<?php
 
session_start();
if (!isset($_SESSION['testnum'])) {
   $_SESSION['testnum'] = 1;
} else {
   ++$_SESSION['testnum'];
}
 
echo $_SESSION['testnum'];

Open in new window

Commented:
I am guessing your login page uses a form, you post the data to formname.php and then from there it redirects to the relevant page depending on the success of the credentials.


This would mean you have 3 pages, the login page, the post form and then the outcome page. Do all 3 have the sessionstart?

Are you running the script on the same server? Is sessions enabled in phpconfig?

Author

Commented:
Thanks for the suggestions.  I don't think the code above is complete (only 10 lines can be copied).

The form is directed to itself so there are only the two pages.  My web host may have changed servers, I'm not sure.  And I don't know if I have access to the phpconfig file.  I'll need to check on that.

It sounds like the Sessions may be turned off though...
Top Expert 2007

Commented:
The code is complete if you create a page test.php with that code and call the page multiple times and the counter does not increase the session management on the server is broken.

Demonstration:
http://ee.jojo.is-a-geek.net/~hernst/ee/23116055.php

Author

Commented:
Thanks hernst42.  After I wrote the note, I actually checked it and saw that the only thing missing was the closing ?>

Sorry to imply that you didn't give the complete code!

I check it and on my server the number does not increment.  It stays on 1.  Which proves that something is going on...

Still checking on the PHPConfig file...
Top Expert 2007

Commented:
Are you sure you haven't blocked cookies from the domain your are testing the sessions on? Else the errorlog should indicate what's going wrong with the php-installation

Author

Commented:
I see my web host gives me access to a PHP.ini file.  Is that the same thing?  It appears that Sessions are turned on.

I forgot to mention that another check I did early on was to use the session_name() function to return the session name.  It was the same on both pages (PHPSESSID) and matches what is listed in the PHP.ini file.

Does that provide any proof that Sessions are turned on?

Author

Commented:
I use both the Session variable and cookies for this script.  The cookies are working as expected.  I have double-checked no cookie blockage.
Top Expert 2007
Commented:
Then check the error-log of the webserver, maybe a disk is full or something else happens with the session-data-file

Author

Commented:
Upon reviewing the error logs for the server I found that the variable 'session.save_path' in the PHP.ini file was pointing to a directory that didn't exist.  Changing the directory fixed everything.  Thanks for your help!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.