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

Post by kta »

I'll definatly take your advice and get the debugger. But just for the hell of it:

Here are the last few lines from page 1. count($_SESSION) returns 6 as it should.

Code: Select all

<?php
     echo "<tr><td align='center' colspan='3'><a href='page2.php'>Page Two</a></td></tr>";
    echo "</table>";
    echo "<p>";
    echo "The size of the SESSIONS array is: ", count($_SESSION);
?>
</body>
</html>

Here are the first few lines from page 2. count($_SESSION) returns 0.

Code: Select all

<?php
   error_reporting(E_ALL);
   ini_set('display_errors', TRUE);
   session_start();
   header("Cache-control: private");
   echo "Size of SESSION array is: ", count($_SESSION);
?>

I see no problems with this code. Oh well. Thanks guys. Time for a break.
kta
Forum Newbie
Posts: 12
Joined: Fri Oct 24, 2003 5:35 pm

Post by kta »

FIXED!

Apparently you can't use numbers as array identifiers only letters. For example:

$_SESSION['12'] = "cow"; This will work fine for the page you are on but can not be saved in the session data file. (this is what I was doing).

$_SESSION['twelve'] = "cow"; Works just fine.

Why is PHP written this way? It seems that numerics are commonly used in this manner in other languages.

In my script I'm writing lots of data into the $_SESSION array using for loops. $_SESSION[$i]="some data"; So this is going to cause some problems. I'll have to sit down and think about this. Thanks again for all your help guys.
- Evan
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

uh, right. And I couldn't find a bug report for that (which doesn't mean there is one ;) )
Maybe you want to post one?
Seems to affect only the top-level of $_SESSION

Code: Select all

<?php
session_start();
$i = count($_SESSION);
$_SESSION[] = date('D M j G:i:s T Y');
$n = count($_SESSION);
?>
<html>
	<head>
		<title>session test</title>
	</head>
	<body>
		<pre><?php echo($i); ?></pre>
		<pre><?php print_r($_SESSION); ?></pre>
		<pre><?php echo($n); ?></pre>
		<a href="<?php echo $_SERVER['PHP_SELF']; ?>">refresh</a>
	</body>
</html>
doesn't work
But

Code: Select all

<?php
session_start();
$i = count(@$_SESSION['whatever']);
$_SESSION['whatever'][] = date('D M j G:i:s T Y');
$n = count(@$_SESSION['whatever']);
?>
<html>
	<head>
		<title>session test</title>
	</head>
	<body>
		<pre><?php echo($i); ?></pre>
		<pre><?php print_r($_SESSION); ?></pre>
		<pre><?php echo($n); ?></pre>
		<a href="<?php echo $_SERVER['PHP_SELF']; ?>">refresh</a>
	</body>
</html>
does
(tested with PHP Version 4.3.3/win32 as module for Apache 2.0.47
and PHP Version 4.3.3/2.4.20-gentoo as module for Apache 2.0.47)
Post Reply