PHP浏览器不会删除cookie

时间:2016-10-05 18:25:52

标签: php cookies

我正在登录系统,但我希望他们能够退出当然,这让我发生了一个大问题,我设置的cookie不想被删除无论我尝试什么,我试过很多方法去除它。

--- 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>

对于我的logout.php我有这个

<?php 

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

我不确定为什么它不想删除cookie,所以我希望你能帮助我。

2 个答案:

答案 0 :(得分:1)

如果要删除Cookie,则不能删除它们。 您必须在过去创建Cookie ...

#include <stdio.h>
#include <cs50.h>

void sort(int arrayRack[], int NumberOfArrayElements);

int main(void)

{
//variable declarations
int NumberOfArrayElements = 0;


//Get number of arrays
printf("number of arrays you'd like?\n");
NumberOfArrayElements = GetInt();

//variable array declaration
int arrayRack[NumberOfArrayElements];


//Get number for each int
for(int i = 0; i < NumberOfArrayElements; i++)
    {
        printf("give me an int for the %i th array\n", i + 1);
        arrayRack[i] = GetInt();
    }

//print what is currently in the arrayRack
for(int j = 0; j < NumberOfArrayElements; j++)
    printf("{%i}<-- number in %i-th array (unsorted)\n", arrayRack[j], j + 1);



for(int i = 0; i < NumberOfArrayElements; i++)
    {
        sort(&arrayRack[i], NumberOfArrayElements);
        printf("{%i}<-- number in %i-th array (sorted)\n", arrayRack[i], i + 1);

    }
    return 0;
}


//this is the funciton, like sort(haystack, size);
void sort(int arrayRack[], int NumberOfArrayElements)
{
//declare variable
int swap = 0;
        for(int k = 0; k < NumberOfArrayElements; k++)
        {
            //if [1] is higher, swap with [2]
            if(arrayRack[k] > arrayRack[k + 1])
            {
                swap = arrayRack[k];
                arrayRack[k] = arrayRack[k + 1];
                arrayRack[k + 1] = swap;
            }
        }

}

答案 1 :(得分:-1)

    $past = time() - 3600;
    setcookie("LoggedIn", gone, $past);
    header("Location: index.php"); exit();