please help me understand this code for logging out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
username0
Forum Newbie
Posts: 4
Joined: Sat Feb 11, 2012 4:58 pm

please help me understand this code for logging out

Post by username0 »

Someone gave me this script for logging out the user and redirect to another page:

Code: Select all

<?php
ob_start();
$_SESSION = array();
// invalidate session cookie
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-86400, '/');
}
// end session then redirect
session_destroy();
header('Location: /index.php');
ob_flush();
exit;
?>
I'm not sure about the purpose of ob_start() and ob_flush().
Also, why $_SESSION = array()?
Thanks.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: please help me understand this code for logging out

Post by social_experiment »

$_SESSION = array() sets the $_SESSION variable to an empty array so everything is 'unset' (all session variables are destroyed);

http://www.php.net/manual/en/function.ob-flush.php
Have a look at the php manual re ob_flush() and ob_start(); I have a vague idea about what they do but not enough to tell you with certainty
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply