?
Solved

PHP Session Variables not staying set on next page

Posted on 2008-01-28
13
Medium Priority
?
671 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!
0
Comment
Question by:jfrent
  • 6
  • 5
  • 2
13 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 20758494
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.
0
 
LVL 8

Expert Comment

by:Lotok
ID: 20758522
Yup, its always handy to have session_start() in your config.php which gets included on each page.
0
 
LVL 2

Author Comment

by:jfrent
ID: 20759524
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 48

Expert Comment

by:hernst42
ID: 20759649
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

0
 
LVL 8

Expert Comment

by:Lotok
ID: 20760509
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?
0
 
LVL 2

Author Comment

by:jfrent
ID: 20762910
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...
0
 
LVL 48

Expert Comment

by:hernst42
ID: 20762960
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
0
 
LVL 2

Author Comment

by:jfrent
ID: 20763131
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...
0
 
LVL 48

Expert Comment

by:hernst42
ID: 20763148
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
0
 
LVL 2

Author Comment

by:jfrent
ID: 20763149
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?
0
 
LVL 2

Author Comment

by:jfrent
ID: 20763180
I use both the Session variable and cookies for this script.  The cookies are working as expected.  I have double-checked no cookie blockage.
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 20763188
Then check the error-log of the webserver, maybe a disk is full or something else happens with the session-data-file
0
 
LVL 2

Author Comment

by:jfrent
ID: 20766816
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!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Over time, the online landscape has altered considerably, but that’s nothing compared to the up-and-coming trends that will shape the web design industry in the coming year. Keep reading to find out which trends will shape B2B web design in 2018.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question