Click to See Complete Forum and Search --> : Sessions
Cactus Hugger
06-02-2003, 09:39 PM
Perhaps I don't quite understand sessions yet... but why does this code always output: "0Page" ??? Thanks in advance.
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
echo $_SESSION['count'];
echo "<a href=\"stest.php\">Page</a>";
?>
Also: I got this from the PHP.net site, so.... why does it not work?
If you look at the logic in the code, it is like this. It starts the session, checks to see if the variable $_SESSION['count'] has a value. If it does, it changes the variable ($_SESSION['count']) to equal zero, if it doesn't, the variable $_SESSION['count'] will increment (meaning if it was 0, it will be 1, if it was 1, it will be 2, etc.). Then it writes to the page the variable's value, and a link to "stest.php." Now then, you'll realize that you have no <br> tag in there, so the variable will be written right along side of the link. Also, in this function, unless the session variable "$count" has a value, it will become zero. Therefore, try putting ?count=1 at the end of your page and re-running the script. Then test the results.
I've never used sessions before, but this is just what it looks like to me..
Jona
Wait, take off all of that. The code works better than you know. Run the code and click the link. Each time the number will increment.
Jona
Cactus Hugger
06-02-2003, 10:54 PM
That's weird. I click the link and it stays at zero.
Are there any PHP settings in the php.ini file (or elsewhere) that could effect this?
Go back and look at the page that gave you that code.
Jona
Cactus Hugger
06-02-2003, 11:26 PM
Fixed! I went through the php.ini stuff for sessions, reading all the comments in the file, and finally found it: session.save_path. I set that to a folder, and it works!