2

我正在尝试编写一个调试脚本来测试我的 cookie,但由于某种原因,它总是运行脚本以删除所有 cookie,即使不满足条件也是如此。这是我的代码 - 都是 html 和 php:

<form id="cookietest" name="CookieTest" method="post">
    <input type="submit" name="createcookie" value="Create cookie" />
    <input type="submit" name="removecookie" value="Remove cookie" />
    <input type="submit" name="removeallcookies" value="Remove all cookies" />
</form>
<?php
    $Domain = thedomain.com
    if($_POST['createcookie'] == TRUE)
    {
        $expire = 60*60*24*14 + time();
        setcookie('TestCookie', 'IExist', $expire, '/', $Domain);
        echo 'TestCookie should have been created. See its value below.<br />';
    }
    if($_POST['removecookie'] == TRUE)
    {
        $expire = time() - 60*60*24*14;
        setcookie('TestCookie', 'IExist', $expire, '/', $Domain);
        echo 'TestCookie should have been deleted. See its value below.<br />';
    }
    if($_POST['removeallcookies'] == TRUE);
    {
        $expire = time() - 60*60*24*14;
        setcookie('TestCookie', 'IExist', $expire, '/', $Domain);
        setcookie('Cookie1', '', $expire, '/', $Domain); // destroys persistent cookie
        setcookie('Cookie2', '', $expire, '/', $Domain); // destroys persistent cookie
        setcookie('Cookie3', '', $expire, '/', $Domain); // destroys persistent cookie
        echo 'All specified cookies should have been removed. <br />';
    }
    if($_POST['createcookie'] == FALSE && $_POST['removecookie'] == FALSE)
    {
        echo 'Neither the cookie create nor cookie remove button was clicked.<br />';
    }
    echo "The contents of 'TestCookie':";
    echo "${_COOKIE['TestCookie']}<br />";
    echo "The cookie array<br />";
    echo "<pre>";
        print_r($_COOKIE);
    echo "</pre>";
?>
4

3 回答 3

5

semi-colon 在 if 条件之后添加了这就是您的 if 块未执行的原因

if($_POST['removeallcookies'] == TRUE);  //<<< semi-colon 
于 2013-01-05T20:29:10.433 回答
3

我认为你应该使用isset而不仅仅是 a ==

if(isset($_POST['createcookie']) )
{
}
于 2013-01-05T20:29:23.520 回答
0

正如@Cthulhu所说,改为使用:

检查它是否已设置:

if(isset($_POST[index])){


// do something

}

检查是否未设置:

if(!isset($_POST[index])){


// do something

}
于 2013-01-05T20:31:24.750 回答