0

I'm making a log in system, however i want them to be able to log out of course, this had me occur a big problem, the cookie i set does not want to be removed no matter what i try, i've tried removing it many ways.

--- Login.php

<!DOCTYPE html>
<html>
<head>
    <title>Website</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css"/>
</head>
<body>
    <header><p> Lost Story </p></header>
    <div id="nav">
        <ul>
            <li><a href="../index.php">Home</a></li>
            <li><a href="register.php">Register</a></li>
            <li><a href="../downloads.html">Downloads</a></li>
            <li><a href="../forums.html">Forums</a></li>
            <li><a href="../donate.html">Donate</a></li>
            <li><a href="../vote.html">Vote</a></li>
                <div id="right">
                <?php
                    if(isset($_COOKIE['LoggedIn']) &&   !empty($_COOKIE['LoggedIn'])) {
                    echo "<li><a href=\"logout.php\">Log Out</a></li>";
                    } else {
                        echo "<li><a href=\"login.php\">Log in</a></li>";
                    }
                ?>
                </div>
        </ul>
    </div>
    <div id="content">
    <?php

    echo var_dump($_COOKIE['LoggedIn']);

    if(isset($_COOKIE['LoggedIn']) && !empty($_COOKIE['LoggedIn'])) {
        echo "<label> You are already logged in, you do not need to again. </label>";
    } else {
        echo "<div id=\"form\">
        <form action=\"login.php\" method=\"POST\">
            <label>
            <br>Username:<br>
                <input type=\"text\" name=\"name\">
            </label>
            <label>
            <br>Password:<br>
                <input type=\"password\" name=\"pass\">
        </label>
        <label>
            <br>
                <input type=\"submit\" name=\"submit\">
        </label>
        </form> 
    </div>";
    }

$LoggedIn = false;

ERROR_REPORTING(E_ALL);
include_once('dbconnection.php');

if($_SERVER['REQUEST_METHOD'] == "POST") {

$user = htmlspecialchars($_POST['name']);
$pass = htmlspecialchars($_POST['pass']);

$once = false;

if(!isset($user) || !isset($pass)) {    
    echo "<p> Please insert all the data </p>";
} else {
    $sql = "SELECT * FROM persons"; 
    $result = mysqli_query($mysqli, $sql);

    if(mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            if($row["username"] === $user && $row["pass"] === md5($pass)) {
                echo "<br><label> Logged into " .$user . "</label>";
                $once = true;
                $LoggedIn = true;
                setcookie("LoggedIn", $LoggedIn == true ? $user : "", time() + (86400 * 30), "/");

            }
        }
        if($once == false ) {
            echo "<br><label>No data found, please insert correct data</label>";
                $once = true;
            }

        $once = false;
    } else {
    echo "No results found";
    }   
}
}
?>

    </div>
    </div>
  <footer> Website copyright etc </footer>
</body>

And for my logout.php i had this

<?php 

if(isset($_COOKIE['LoggedIn']) && !empty($_COOKIE['LoggedIn'])) {
    setcookie('LoggedIn', '', time()-3600, '/');
    header("Location: ../index.php");
} else {
    echo "no";
}
?>

I'm not really sure why it does not want to remove the cookies, so i hope you can help me.

  • 1: you don't need to echo the var_dump. 2: You have no code to unset the cookie. – MikeVelazco Oct 05 '16 at 18:31
  • @MikeVelazco Every stackoverflow post i went to told me to set the cookie in the past to unset it though –  Oct 05 '16 at 18:53

2 Answers2

1

When you want to delete your cookies, you can't just delete them. You must create the cookie in the past...

<?php
// set the expiration date to one hour ago
setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);
?>
Koen Hollander
  • 1,687
  • 6
  • 27
  • 42
  • I see, thank you for the useful information for the future, you've got quite the skill! –  Oct 05 '16 at 18:56
-1
    $past = time() - 3600;
    setcookie("LoggedIn", gone, $past);
    header("Location: index.php"); exit();