110 likes | 227 Vues
This article explores session management in PHP, focusing on how to maintain state between HTTP transactions using sessions. It details the generation of unique session IDs, storage of session data in cookies, and how to create and manipulate session variables using the superglobal `$_SESSION`. Examples provided in the text illustrate creating, accessing, and destroying session variables across multiple pages (Page 1, Page 2, Page 3). You'll gain an understanding of session lifecycle, including starting sessions, updating session data, and cleaning up when done.
E N D
Using Session Control in PHP Chapter 23
No built in way of maintaining a state between two transactions HTTP is a stateless protocol
Unique session ID – random number Generated by PHP Stored on client for the lifetime of the session Stored as a cookie PHP Sessions
<h1>Page 1</h1> <?php session_start(); $_SESSION['sess_var'] = "Hello World"; echo 'The contents of $_SESSION[\'sess_var\'] is ' .$_SESSION['sess_var'].'<br />'; ?> <a href="page2.php">Next page</a> Page1.php
Checks to see whether there is a current session. If not, it not it will create one. Start_session
Superglobal array $_SESSION • To create a session variable: • $_SESSION[‘id’] = 99; • When finished • unset($_SESSION[‘id’]); Session variables
session_destroy( ); Finish the session
<h1>Page 1</h1> <?php session_start(); $_SESSION['sess_var'] = "Hello World"; echo 'The contents of $_SESSION[\'sess_var\'] is ' .$_SESSION['sess_var'].'<br />'; ?> <a href="page2.php">Next page</a> Page1.php
http://cscdb.nku.edu/csc301/frank/Chapter23/page1.php Page1.php
<h1>Page 2</h1> <?php session_start(); echo 'The contents of $_SESSION[\'sess_var\'] is ' .$_SESSION['sess_var'].'<br />'; unset($_SESSION['sess_var']); ?> Page2.php
<h1>Page 3</h1> <?php session_start(); echo 'The contents of $_SESSION[\'sess_var\'] is ' .$_SESSION['sess_var'].'<br />'; session_destroy(); ?> Page3.php