I can't pass values using $_SESSION

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

kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

I can't pass values using $_SESSION

Post by kta »

On one page I put values into the $_SESSION array.
example:

$_SESSION['cow']="moo";

after this count($_SESSION) returns 1.

On another page I try to get the values out, but it doesn't work.
example

echo $_SESSION['cow'];

on this page count($_SESSION) returns 0.

All pages start with session_start(). Cookies are enabled and I have verified them being created. The session id IS being passed between pages. What else could be the problem?

I'm stumped. Thanks for the help. -Evan
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Is the session id the same on both pages? echo session_id();

Do you have register_globals On or Off?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Annoying... I did this, and got the results shown last. What does your code look like?

Code: Select all

<?php
// index.php
session_start();
$_SESSION['foo'] = 'bar';
echo 'Count: '.count($_SESSION);
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>
<a href="foo.php">foo.php</a>

<?php
// foo.php
session_start();
echo 'Count: '.count($_SESSION);
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>
<a href="index.php">index.php</a>

/*
My results on both pages:

        Count: 1
        
        Array
        (
            [foo] => bar
        )
*/
kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

Post by kta »

I echo the session id to both pages and they are the same.

I have tried with globals on and off. But this shouldn't matter anyway right? because $_SESSION is "superglobal".

The code I'm using is kind of complex, here are some snippets. Putting data into the $_SESSION array on page 1 works just fine as I can output the data at the end of the page and it is all there:

Code: Select all

<?php
	   $name = $_POST['name'];
	   $style = $_POST['style'];
	   $color = $_POST['color'];
	   $size = $_POST['size'];
	   $quantity = $_POST['quantity'];
	   $price = $_POST['price'];

	      	//Put values into the SESSION array
	   	for ($j=$initialCount; $j <= ($initialCount + 5); $j++ )
	   	{
	        echo "j is ", $j, "<br>";
	   		//always put name in the first of a grouping of six
	   		if (($j % 6) == 1)
	   		{
	   			$_SESSION[$j] = $name;
	   		}
	   		//always put style in the second of a grouping of six
	   		if (($j % 6) == 2)
	   		{
	   			$_SESSION[$j] = $style;
	   		}
	   		//always put color in the third of a grouping of six
	   		if (($j % 6) == 3)
	   		{
	   			$_SESSION[$j] = $color;
	   		}
	   		//always put size in the fourth of a grouping of six
	   		if (($j % 6) == 4)
	   		{
	   			$_SESSION[$j] = $size;
	   		}
	   		//always put quantity in the sixth of a grouping of six
	   		if (($j % 6) == 5)
	   		{
	   			$_SESSION[$j] = $quantity;
	   		}
	   		//always put price in the seventh of a grouping of six
	   		if (($j % 6) == 0)
	   		{
	   			$_SESSION[$j] = ($price * $quantity);
	   		}
	   	}
?>
Once the values are put into the $_SESSION array I do not change or remove any of them. On page 2 non of the values are there anymore. Like I said before, count($_SESSION) now returns 0 where it once had many values in it.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Where is session_start() ?
You might need that on all pages handling the sessions...
kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

Post by kta »

like I stated in the original post I have session_start(); at the top of all my pages. That code is just a couple snippets I took out of a much larger block.

I guess what I would like to know is if there are any settings in php.ini or elsewhere that could cause this problem that haven't been mentioned yet. Or if the problem is more likely in my code. Thanks again - Evan
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sorry, just making really sure...

There are some settings, as;
session.cookie_lifetime = 0
session.cache_expire = 180
etc.
...but I hardly think this is the case, as a good host would not mess with them (generally). Sorry, can't think of anything that would be helpful here... =/
kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

Post by kta »

Something I just noticed. If I open up a page it creates the cookie "sess_sessionid" but when I write something to the $_SESSION array nothing is actually written into the cookie.

So for example. If I do
$_SESSION['cow'] = "moo";
something should be written into the cookie correct? But nothing is being put into it.

Why isn't anything being saved into the cookie? Does this help narrow down an error? Thanks - Evan
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

no, only the session id is written to the cookie. session data is not stored client-side.
By default php uses files to store the data. The filename corresponds to the sessionid
php.ini wrote:[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
session.save_path = ...
session.save_path must point to a valid directory where php can read/write the sessionfiles. If you open such a file the session data in a more or less readable format should be in there.
What happens if you run your scripts with

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
before session_start()?
kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

Post by kta »

I guess thats what I was looking at. Not the cookie but the session file (server side). Nothing is being put into it.

adding the error reporting lines to my scripts produces nothing. ARG
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hm, which version of php do you use?
kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

Post by kta »

I'm running 4.3.3 . The site says it's a stable release. Should I try an older version?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I just thought you might use an old version that does not support $_SESSION at all.
Does this script

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);

session_start();
@$_SESSION['counter'] += 1;
session_write_close();
// restart session
session_start();
?>
<html>
	<head>
		<title>session test</title>
	</head>
	<body>
<?php
echo 'session counter: ', @$_SESSION['counter'];
session_write_close();
?>
		<a href="<?php echo $_SERVER['PHP_SELF']; ?>">refresh</a>
	</body>
</html>
work?
kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

Post by kta »

yes the above script does works. and I made another page like this

Code: Select all

<?php
<?php
     error_reporting(E_ALL);
     ini_set('display_errors', TRUE);
     session_start();
?>
<html>
<head>
<title>Title here!</title>
</head>
<body>
<?php
     echo "value in counter: ", $_SESSION['counter'];
?>
</body>
</html>


?>
and that works too. So I guess the error is in my code somewhere. damn.

Are the session_write_close() tags necessary? I took them out and it still seems to work fine. Thanks for the help guys. I guess its time to do some serious debugging.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

session_write_close() is not necessary, it happens automagically on script-termination. I just wanted to make sure it's not a problem with write-permissions, therefor
start->alter value->stop->start again->read value
all within one request so the client isn't involved (at this stage)
I guess its time to do some serious debugging.
so it would seem ;)
Probably the right time to take the extra mile and setup a debugger extension (if you can)
take a look at http://www.php.net/manual/en/debugger.php
it's worth the time and much more powerful than a printf/echo "debugger"
Post Reply