logout.php

The final ingredient in our social networking recipe is Example 21-12, logout.php, the logout page that closes a session and deletes any associated data and cookies. The result of calling up this program can be seen in Figure 21-8, where the user is now asked to click on a link that will take that user to the un-logged-in home page and remove the logged-in links from the top of the screen. Of course, you could write a JavaScript or PHP redirect to do this (probably a good idea if you wish to keep logging out looking clean).

Example 21-12. logout.php
<?php // logout.php
include_once 'header.php';

if (isset($_SESSION['user']))
{
    destroySession();
    echo "<div class='main'>You have been logged out. Please " .
         "<a href='index.php'>click here</a> to refresh the screen.";
}
else echo "<div class='main'><br />" .
          "You cannot log out because you are not logged in";
?>

<br /><br /></div></body></html>
The logout page
Figure 21-8. The logout page

Get Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.