I'm trying to create a sign out button with php. I might be going about this the completely wrong way, but anyhow I'm trying to delete a cookie on the click of a button.
<?php
function logOut() {
setcookie('userDetails[username]',"", time()-1200);
}
if(isset($_COOKIE["userDetails"])) {
echo "<table id='rtgLoginBox'>" ;
echo "<tr>" ;
echo "<td> Cookie " . $_COOKIE["userDetails"]["username"] . "!";
echo "<td></td>" ;
echo "</tr><tr>" ;
echo "<td><input id='rtgLoginBtn' type='submit' value='Log Out' onclick=\"logOut();\" /></td>" ;
echo "<td>";
echo "<td></td>" ;
echo "<td></td>" ;
echo "</tr><tr>" ;
echo "<td></td>" ;
echo "<td></td>" ;
echo "</tr>" ;
echo "</table>" ;
} else {
echo "<table id='rtgLoginBox'>" ;
echo "<tr>" ;
echo "<td><form action='process.php' method='post'>Username:</td>" ;
echo "<td><input name='usernamein' id='rtgUsernameTxt' type='text' size='20' class='rtgSignUpTxt'/></td>" ;
echo "</tr><tr>" ;
echo "<td>Password:</td>" ;
echo "<td><input name='passwordin' id='rtgPasswordTxt' type='password' size='20' class='rtgSignUpTxt' /></td>" ;
echo "</tr><tr>" ;
echo "<td></td>" ;
echo "<td><input id='rtgLoginBtn' type='submit' value='Log In' /> </form><input id='rtgLoginBtn' type='submit' value='Sign Up' onclick=\"location.href='SignUp.php'\" /></td>" ;
echo "</tr><tr>" ;
echo "<td></td>" ;
echo "<td><a href='mysite.com'><p style='padding:0 !important;'>Forgot Login Details?</p></a></td>" ;
echo "</tr>" ;
echo "</table>" ;
}
?>
As far as I've read online, this should work? I also tried putting the function under the echoed table, but this didn't work either.
This code will cause the person to logout if they press the logout button and then redirect them to a new page called whatever.php. You obviously can change that to be whatever you want.
<?php
function logOut() {
setcookie('userDetails[username]',"", time()-1200);
unset($_COOKIE['userDetails']);
}
if ('POST' === $_SERVER['REQUEST_METHOD'])
{
logOut();
header('Location: /whatever.php');
exit;
}
if(isset($_COOKIE["userDetails"])) {
echo "<form action='".htmlspecialchars($_SERVER['PHP_SELF'])."' method='post'><table id='rtgLoginBox'>" ;
echo "<tr>" ;
echo "<td> Cookie " . $_COOKIE["userDetails"]["username"] . "!";
echo "<td></td>" ;
echo "</tr><tr>" ;
echo "<td><input id='rtgLoginBtn' type='submit' value='Log Out' /></td>" ;
echo "<td>";
echo "<td></td>" ;
echo "<td></td>" ;
echo "</tr><tr>" ;
echo "<td></td>" ;
echo "<td></td>" ;
echo "</tr>" ;
echo "</table></form>" ;
} else {
echo "<table id='rtgLoginBox'>" ;
echo "<tr>" ;
echo "<td><form action='process.php' method='post'>Username:</td>" ;
echo "<td><input name='usernamein' id='rtgUsernameTxt' type='text' size='20' class='rtgSignUpTxt'/></td>" ;
echo "</tr><tr>" ;
echo "<td>Password:</td>" ;
echo "<td><input name='passwordin' id='rtgPasswordTxt' type='password' size='20' class='rtgSignUpTxt' /></td>" ;
echo "</tr><tr>" ;
echo "<td></td>" ;
echo "<td><input id='rtgLoginBtn' type='submit' value='Log In' /> </form><input id='rtgLoginBtn' type='submit' value='Sign Up' onclick=\"location.href='SignUp.php'\" /></td>" ;
echo "</tr><tr>" ;
echo "<td></td>" ;
echo "<td><a href='mysite.com'><p style='padding:0 !important;'>Forgot Login Details?</p></a></td>" ;
echo "</tr>" ;
echo "</table>" ;
}
?>