1 / 14

PHP Session

PHP Session. ISYS 475. Session. The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session. Session support in PHP consists of a way to preserve certain data across subsequent accesses in the $_SESSION superglobal array.

teva
Download Presentation

PHP Session

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. PHP Session ISYS 475

  2. Session • The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session. • Session support in PHP consists of a way to preserve certain data across subsequent accesses in the $_SESSION superglobal array. • When a visitor accesses your site, PHP will check automatically or on your request (explicitly through session_start() ) whether a specific session id has been sent with the request. If this is the case, the prior saved data in $_SESSION is recreated.

  3. session_start() • Start new or resume existing session

  4. Example: Tracking the number of times a button is clicked <?php if (isset($counter)) $counter=$counter+1; else $counter=0; ?> <form action="callSelf.php"> <?php if ($counter>0) echo "You click me $counter times!<br><br>"; else $counter=0; ?> <input type="submit" value="Click Me" name="btnClick" /> </form> Note: This program does not work because the value of $counter is lost.

  5. Correction: Save $counter in $_SESSION <?php session_start(); if (isset($_SESSION["counter"])) { $counter=$_SESSION["counter"]+1; $_SESSION["counter"]=$counter; } else { $counter=0; $_SESSION["counter"]=$counter; } ?> <form action="callSelf.php"> <?php if ($counter>0) echo "You click me $counter times!<br><br>"; ?> <input type="submit" value="Click Me" name="btnClick" /> </form>

  6. Example: Save a variable in $_SESSION and access it from other pages • In Page 1, add variable $user in $_SESSION • Page 2 and Page 3 read variable $user and show a message.

  7. Page 1 <?php session_start(); if (isset($_SESSION["user"])) $user=$_SESSION["user"]; else { $user="David"; $_SESSION["user"]=$user; } echo " Hello, $user, you are visiting page 1!<br>"; ?> <br> <a href="page2.php">Visit page 2 click here</a><br> <a href="page3.php">Visit page 3 click here</a><br>

  8. Page 2, 3 <?php session_start(); if (isset($_SESSION["user"])) $user=$_SESSION["user"]; echo "Hello, $user, you are visiting page 2!<br>"; ?> <br> <a href="page1.php">Visit page 1 click here</a><br> <a href="page3.php">Visit page 3 click here</a><br> <?php session_start(); if (isset($_SESSION["user"])) $user=$_SESSION["user"]; echo "Hello, $user, you are visiting page 3!<br>"; ?> <br> <a href="page1.php">Visit page 1 click here</a><br> <a href="page2.php">Visit page 2 click here</a><br>

  9. Example: Use an array to save all the pages user visited • The array is saved in $_SESSION

  10. Page 1 <?php session_start(); if (isset($_SESSION["user"])) $user=$_SESSION["user"]; else { $user="David"; $_SESSION["user"]=$user; } if (isset($_SESSION["visited"])) { $visited=$_SESSION["visited"]; $visited[count($visited)]="page1"; } else $visited[0]="page1"; $_SESSION["visited"]=$visited; echo " Hello, $user, you are visiting page 1!<br>"; var_dump($visited); ?> <br> <a href="page2.php">Visit page 2 click here</a><br> <a href="page3.php">Visit page 3 click here</a><br>

  11. Page 2 <?php session_start(); if (isset($_SESSION["user"])) $user=$_SESSION["user"]; if (isset($_SESSION["visited"])) { $visited=$_SESSION["visited"]; $visited[count($visited)]="page2"; } else $visited[0]="page2"; $_SESSION["visited"]=$visited; echo "Hello, $user, you are visiting page 2!<br>"; var_dump($visited); ?> <br> <a href="page1.php">Visit page 1 click here</a><br> <a href="page3.php">Visit page 3 click here</a><br>

  12. Page 3 <?php session_start(); if (isset($_SESSION["user"])) $user=$_SESSION["user"]; if (isset($_SESSION["visited"])) { $visited=$_SESSION["visited"]; $visited[count($visited)]="page3"; } else $visited[0]="page3"; $_SESSION["visited"]=$visited; echo "Hello, $user, you are visiting page 3!<br>"; var_dump($visited); ?> <br> <a href="page1.php">Visit page 1 click here</a><br> <a href="page2.php">Visit page 2 click here</a><br>

  13. Session_id() • session_id() is used to get or set the session id for the current session. <?php session_start(); echo " session id is:" . session_id(); ?>

  14. To Stop a Session: session_destroy() • session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called. <?php session_start(); session_destroy(); header("location:login.php"); ?>

More Related