Logging out

Difficulty: Super easy

This is just something very quick and basic. Here is a logout code. I added in comments.

[php]<?php
session_start();
//That continues on the started session

session_destroy();
//That destroyed the session

header(‘Refresh: 3; index.php’);
exit();
//That means that you will be redirected to the main page in 3 seconds

echo “Logged out. Redirecting in 3 seconds”;
//That should be straightforward

?>[/php]

I know this is very basic.

If you are storing any information in your settings (like username, password, ID#) it’s could to unset those as well! Adding on to your code here:

[php]session_start();

unset($_SESSION[‘username’]);
unset($_SESSION[‘email’]);
unset($_SESSION[‘password’]);
$_SESSION = array();
session_destroy();

header(‘Refresh: 3; index.php’);
exit();

echo “Logged out. Redirecting in 3 seconds”;

[/php]

Not sure, but, shouldn’t you echo the 3 second notice before you header it out?
Don’t think it will be seen without using it before exiting…

I think your right as the refresh will show the message but the exit command will stop the script going any further, is it won’t get to the echo at all.

Sponsor our Newsletter | Privacy Policy | Terms of Service