The document discusses PHP sessions including starting a session, setting session variables, getting session variable values, and destroying a session. Sessions are used to maintain user state on a web server.
The document discusses PHP sessions including starting a session, setting session variables, getting session variable values, and destroying a session. Sessions are used to maintain user state on a web server.
The document discusses PHP sessions including starting a session, setting session variables, getting session variable values, and destroying a session. Sessions are used to maintain user state on a web server.
The document discusses PHP sessions including starting a session, setting session variables, getting session variable values, and destroying a session. Sessions are used to maintain user state on a web server.
sessions are used Starting session • A session is started with the session_start() function. • Session variables are set with the PHP global variable: $_SESSION. • The session_start() function must be the very first thing in your document. Before any HTML tags. • Unique SessionID is created for each visitor • String function session_id() returns current session_id • <?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variables are set."; ?> </body> </html> Get value of session variables • all session variable values are stored in the global $_SESSION variable: <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Echo session variables that were set on previous page echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>"; echo "Favorite animal is " . $_SESSION["favanimal"] . "."; ?> </body> </html> Destroy a PHP Session • To remove all global session variables and destroy the session, use session_unset() and session_destroy(): • <?php session_start(); ?> <html> <body> <?php // remove all session variables session_unset(); // destroy the session session_destroy(); ?> </body> </html>