1 / 11

Managing Sessions in PHP: A Practical Guide

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.

thuyet
Download Presentation

Managing Sessions in PHP: A Practical Guide

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Session Control in PHP Chapter 23

  2. No built in way of maintaining a state between two transactions HTTP is a stateless protocol

  3. Unique session ID – random number Generated by PHP Stored on client for the lifetime of the session Stored as a cookie PHP Sessions

  4. <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

  5. Checks to see whether there is a current session. If not, it not it will create one. Start_session

  6. Superglobal array $_SESSION • To create a session variable: • $_SESSION[‘id’] = 99; • When finished • unset($_SESSION[‘id’]); Session variables

  7. session_destroy( ); Finish the session

  8. <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

  9. http://cscdb.nku.edu/csc301/frank/Chapter23/page1.php Page1.php

  10. <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

  11. <h1>Page 3</h1> <?php session_start(); echo 'The contents of $_SESSION[\'sess_var\'] is ' .$_SESSION['sess_var'].'<br />'; session_destroy(); ?> Page3.php

More Related