sonnysavage
Programmer
I've seen a LOT of session-related questions lately, so I thought I'd write this little tip.
To start a session:
To set data into a session:
$_SESSION is used just like any other PHP variable with a few exceptions. When you call session_start() in subsequent pages, the values you've set get restored. Secondly, and very conveniently, it is an "autoglobal". This means that you don't have to do anything special to access it's values in functions or classes.
Example:
Feel free to add corrections and/or errata. Enjoy!
To start a session:
Code:
session_start();
To set data into a session:
Code:
$_SESSION["value_identifier"] = "value";
$_SESSION is used just like any other PHP variable with a few exceptions. When you call session_start() in subsequent pages, the values you've set get restored. Secondly, and very conveniently, it is an "autoglobal". This means that you don't have to do anything special to access it's values in functions or classes.
Example:
Code:
<?php
session_start();
$_SESSION["message"] = "Hello world!";
function Message() {
return "Function: ".$_SESSION["message"]."<br />";
}
class Message {
function Get() {
return "Object: ".$_SESSION["message"]."<br />";
}
}
echo $_SESSION["message"]."<br />";
echo Message();
$Message = new Message();
echo $Message->Get();
?>
Feel free to add corrections and/or errata. Enjoy!