470 likes | 1.36k Views
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.
E N D
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. • 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.
session_start() • Start new or resume existing session
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.
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>
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.
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>
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>
Example: Use an array to save all the pages user visited • The array is saved in $_SESSION
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>
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>
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>
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(); ?>
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"); ?>