Today you will learn how to create a log out script in PHP. This applies only for those authentication systems that are based on cookie existence and evaluation. The article will cover the steps that are to be followed when the log in script sets for example a hash cookie and then does some evaluations to corroborate that the user is logged in.
1. Let’s say when you log in, your script sets two cookies: email & hash. The email cookie stores the email you use to log in, while the hash cookie stores a randomly generated hash. This hash is stored in a field of your email’s row and checks that the hash in the cookie equals to the one in the DB.
2. To accomplish a log out, you simply set new cookies but with negative duration. You will most likely set the log out cookie to a great negative duration, to avoid the cookie not being destroyed. As you created the code for the log in cookies, you will do for the log out as follows:
setcookie('email', '', time()-60*60*24*365, '/'); setcookie('hash', '', time()-60*60*24*365, '/');
3. Save that code inside a PHP file. I would save it in logout.php. Upload to web or local server.
4. Log in and then navigate to the logout.php from your browser and manually check that the cookies were removed.
If at any step you get stuck, or happen to have any question, please do not hesitate to contact me. Any comment, article suggestion, etc is welcomed!
Looking for quality web hosting? Look no further than Arvixe Web Hosting!