Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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; ?> <?php if ($counter>0) echo "You click me $counter times! "; else $counter=0; ?> 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; } ?> <?php if ($counter>0) echo "You click me $counter times! "; ?>

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! "; ?> Visit page 2 click here Visit page 3 click here

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

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! "; var_dump($visited); ?> Visit page 2 click here Visit page 3 click here

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! "; var_dump($visited); ?> Visit page 1 click here Visit page 3 click here

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! "; var_dump($visited); ?> Visit page 1 click here Visit page 2 click here

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"); ?>


Download ppt "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."

Similar presentations


Ads by Google