110 likes | 221 Views
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>
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