Session variable data
Moderator: General Moderators
Session variable data
Anyone have any ideas why session variable data is not being kept from page to page? If I store a session variable on the same page and try to view the contents i can see them fine, but if I store the variable on a login page and then try to use on other pages, then the data is gone. Any help is appreciated. Thanks.
Re: Session variable data
You have session_start() at the beginning of every page?
Re: Session variable data
Yes, sorry. I am using the following:
This works fine, but will not travel to another page.
Code: Select all
session_start();
// store session data
$_SESSION['views']=1;Re: Session variable data
But you're still not answering the question: are you using session_start() on every page? We can't speculate on other potential problems until we know that you're at least doing that.kipp wrote:Yes, sorry. I am using the following:
This works fine, but will not travel to another page.Code: Select all
session_start(); // store session data $_SESSION['views']=1;
Re: Session variable data
Sorry, yes on every page that I need to verify the session data.
Re: Session variable data
Then, are your successive pages actually IN the same session? In other words, are you linking directly from one page to the next, as opposed to just typing in a new address in the browser? Assuming that your web server doesn't have sessions somehow turned off (I've never even looked to see if that's possible, but it probably is), I see no reason that session variables wouldn't work as expected. I would make 2 tiny test files, one that sets a session variable and has a link to the other test file that reads the session variable. It often helps to clear away absolutely everything that isn't related to your problem and find out what's really going on.kipp wrote:Sorry, yes on every page that I need to verify the session data.
Re: Session variable data
Yes, the pages are connected to one another..it is a simple (or at least I thought it was) login system. I did try a very basic session system on two new pages with just the basic assign session variables with a number and the try to echo the session variable on another page. It is weird, I have never had problems before.
Re: Session variable data
it's possible the session vars aren't being written to the file system (permissions issue perhaps). If you have control over the server I'd check to make sure they're being written when you create them. You can always look into using database sessions instead of that is indeed your problem.
Re: Session variable data
Here are the two files I used:
and b.php
this returns:
Pageviews =
Code: Select all
session_start();
$_SESSION['views'] = 3; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
header("Location: b.php")
Code: Select all
session_start();
echo "Pageviews = ". $_SESSION['views']; //retrieve data
Pageviews =
Re: Session variable data
How are you calling b.php? The header won't work because (1) you didn't end the php statement with a semicolon and (2) because you have already sent data to the browser with the echo.
Re: Session variable data
I would check whether Sessions are enabled. Create a file phpinfo.php:
Run that. Scroll way down (perhaps halfway) to the section labelled sessions and see if it's enabled or not.
Code: Select all
<?php
phpinfo();
?>Re: Session variable data
Session Support enabled
Registered save handlers files user sqlite
Registered serializer handlers php php_binary wddx
Registered save handlers files user sqlite
Registered serializer handlers php php_binary wddx
Re: Session variable data
Try changing the first page code to output a manual hyperlink ... like ...
b.php should output as desired when clicked. If not, let us know what it says.
Code: Select all
<?php
session_start();
$_SESSION['views'] = 3; // store session data
echo "Pageviews = ". $_SESSION['views'] . '<br>'; //retrieve data
?>
<a href="b.php">Test Click</a>Re: Session variable data
a.php returns:
Pageviews = 3
Test Click
b.php returns:
Pageviews =
Pageviews = 3
Test Click
b.php returns:
Pageviews =
Re: Session variable data
Just out of interest, where is this hosted? If we're talking a hosting service, then your best bet is to follow it up with them because it does sound like something is amiss with either the session file location or the permissions for those files. I say talk to them, because both of these things are pretty much out of your control.
If it is on your own machine, then that's different and we'll have to look at your setup more closely. In particular, you'll want to look at the values in phpinfo() for session.save_handler and session.save_path. These are both listed under the session section. For a local server, you'll want to ensure that the location exists, and that the web server has permission to write files there.
About the only other thing I would suggest at this stage is to make sure that your cookies aren't disabled or being blocked by any anti-virus programs. I'm assuming you are running IE, but do you get the same result with Firefox or Chrome? Just that these function independently of IE's security zones.
Let us know how you go.
If it is on your own machine, then that's different and we'll have to look at your setup more closely. In particular, you'll want to look at the values in phpinfo() for session.save_handler and session.save_path. These are both listed under the session section. For a local server, you'll want to ensure that the location exists, and that the web server has permission to write files there.
About the only other thing I would suggest at this stage is to make sure that your cookies aren't disabled or being blocked by any anti-virus programs. I'm assuming you are running IE, but do you get the same result with Firefox or Chrome? Just that these function independently of IE's security zones.
Let us know how you go.