I have a website where you can log in with a session or with cookies (remember me) and I can't seem to get the logout to work. I have a code that goes on top of most pages (all pages that require one to be logged in) and it goes like this:
<?php
//session
session_start();
//login check function
function loggedin()
{
if (isset($_SESSION['username'])||isset($_COOKIE['username']))
{
$loggedin = TRUE;
return $loggedin;
}
if (!loggedin())
{
header("Location: index.php");
exit();
}
}
if (isset($_SESSION['username']))
{
$loginusername = $_SESSION['username'];
} else if (isset($_COOKIE['username']))
{
$loginusername = $_COOKIE['username'];
}
?>
And my logout code goes like this:
<?php
session_start();
session_destroy();
setcookie("username","",time()-32000);
include($_SERVER['DOCUMENT_ROOT'].'/header.html');
echo "You've been logged out. Click <a href='/'>here</a> to return.";
include($_SERVER['DOCUMENT_ROOT'].'/footer.html');
?>
And whenever I log out, if I go to a page that SHOULD kick me back to home if I am not logged in (in the first code), it doesn't. Also, it shows me as not having a username (which makes sense because I am logged out). So if I don't have a username (I echo $loginusername and it shows up as nothing), how come it's not kicking me back to index.php?