I can't pass values using $_SESSION
Moderator: General Moderators
I can't pass values using $_SESSION
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
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
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
)
*/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:
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.
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);
}
}
?>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
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
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
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
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
What happens if you run your scripts withbefore session_start()?
By default php uses files to store the data. The filename corresponds to the sessionid
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.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 = ...
What happens if you run your scripts with
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', TRUE);I just thought you might use an old version that does not support $_SESSION at all.
Does this scriptwork?
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>yes the above script does works. and I made another page like this
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.
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>
?>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.
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)

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"
start->alter value->stop->start again->read value
all within one request so the client isn't involved (at this stage)
so it would seemI guess its time to do some serious debugging.
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"