Session variable data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kipp
Forum Commoner
Posts: 27
Joined: Fri Jan 09, 2009 1:25 pm

Session variable data

Post by kipp »

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.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Session variable data

Post by papa »

You have session_start() at the beginning of every page?
kipp
Forum Commoner
Posts: 27
Joined: Fri Jan 09, 2009 1:25 pm

Re: Session variable data

Post by kipp »

Yes, sorry. I am using the following:

Code: Select all

session_start();
// store session data
$_SESSION['views']=1;
This works fine, but will not travel to another page.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Session variable data

Post by califdon »

kipp wrote:Yes, sorry. I am using the following:

Code: Select all

session_start();
// store session data
$_SESSION['views']=1;
This works fine, but will not travel to another page.
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
Forum Commoner
Posts: 27
Joined: Fri Jan 09, 2009 1:25 pm

Re: Session variable data

Post by kipp »

Sorry, yes on every page that I need to verify the session data.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Session variable data

Post by califdon »

kipp wrote:Sorry, yes on every page that I need to verify the session 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
Forum Commoner
Posts: 27
Joined: Fri Jan 09, 2009 1:25 pm

Re: Session variable data

Post by kipp »

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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Session variable data

Post by Burrito »

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.
kipp
Forum Commoner
Posts: 27
Joined: Fri Jan 09, 2009 1:25 pm

Re: Session variable data

Post by kipp »

Here are the two files I used:

Code: Select all

 
session_start(); 
$_SESSION['views'] = 3; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
header("Location: b.php")
 
and b.php

Code: Select all

 
session_start(); 
echo "Pageviews = ". $_SESSION['views']; //retrieve data
 
this returns:

Pageviews =
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Session variable data

Post by califdon »

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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Session variable data

Post by califdon »

I would check whether Sessions are enabled. Create a file phpinfo.php:

Code: Select all

<?php
phpinfo();
?>
Run that. Scroll way down (perhaps halfway) to the section labelled sessions and see if it's enabled or not.
kipp
Forum Commoner
Posts: 27
Joined: Fri Jan 09, 2009 1:25 pm

Re: Session variable data

Post by kipp »

Session Support enabled
Registered save handlers files user sqlite
Registered serializer handlers php php_binary wddx
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Session variable data

Post by Stryks »

Try changing the first page code to output a manual hyperlink ... like ...

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>
b.php should output as desired when clicked. If not, let us know what it says.
kipp
Forum Commoner
Posts: 27
Joined: Fri Jan 09, 2009 1:25 pm

Re: Session variable data

Post by kipp »

a.php returns:

Pageviews = 3
Test Click

b.php returns:
Pageviews =
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Session variable data

Post by Stryks »

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.
Post Reply