Forum Moderators: coopster
php sessions dont start automatically, you have to start them once with session_start();
I ran into a more subtle variation on this, which mystified me for ages. Take this code:
<?
session_start();
session_register("my_variable");
$my_variable="Hello world";
header("Location: next.php");
exit;
?>
The session variable will never be set. Why? Because the browser is redirected before the end of the script has been parsed. If you've ever wondered why bulletin boards usually display a "Your message has been sent" page before redirecting with a <meta> refresh... that's the reason.
If session_start() was not called before this function (session_register) is called, an implicit call to session_start() with no parameters will be made.
And yes, the order is imporant, because you can't change the value of a variable after registering it.
Have you tried this?
<?
session_register("variable");
unset($variable);
session_unregister("variable");
$variable = "foobar";
session_register("variable");
header("location: next.php");
exit;
?>
<?
session_register("variable");
echo $variable;
?>
page1;
<?php
session_start();
$myVAR = "23";
session_register("$myVAR");
?>
page2;
<?php
session_start();
$myVAR++;
?>
page3;
<?php
echo "$myVAR"; // returns 24
?>
You can use one page for all this using conditional statements or switch.
But as far as I know it always works for me with no problems, unless you try to re-register the variable.
Of course it works! I tell you, I do it all the time!
Here, from the PHP.net documentation: If session_start() was not called before this function (session_register) is called, an implicit call to session_start() with no parameters will be made.
Ah, but only if your script includes session_register(). It will often be the case that you want to use session variables on a page that doesn't register any; in which case, you must have a call to session_start()
Also, of course you can change the values of a registered session variables. Session variables are variables, which means the values they contained can be changed at any time -- that's what "variable" means. However, I have discovered that if you redirect the browser, under certain conditions which have little to do with the placement of session_register() (I know, because my first thought was that I'd put it in the wrong place, so I moved it), the value of the variable as recorded in the text file that stores session variables is not updated.
And if you change a variable after registering it and then, go to another page, the new page will reflect the new value?
I mean:
-- page1.php --
<?
session_start();
$variable = 3;
session_register("variable");
$variable++;
header ("location: next.php");
exit;
?>
-- rext.php --
<?
session_start();
session_register("variable");
echo $variable;
?>
--
It prints 4!
Sessions are so cool!
No need for the session register on the next page BTW.
Also, there are times when you want a var passed in the url to affect the session: Very easy:
if(£varInUrl="yes") {
$sessionVar="yes";
} else $sessionVsr="no";
Just be careful with sessions and SE's, be aware that it's easy to fall into a 'duplicate content trap'.
Nick