php - 为什么我的 "if"语句总是确定为 TRUE?

标签 php forms button if-statement conditional-statements

我正在尝试编写一个调试脚本来测试我的 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>";
?>

最佳答案

你在 if 条件之后添加了 semi-colon 这就是你的 if block 没有被执行的原因

if($_POST['removeallcookies'] == TRUE);  //<<< semi-colon 

关于php - 为什么我的 "if"语句总是确定为 TRUE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14176053/

相关文章:

angular - 测试 Angular 的 ngForm 形式

android - ON_CANCEL 永远不会被调用

python - 使用 "browse"按钮在 Tkinter 中显示文件路径 - Python

php - 至少 4 个字符或不显示任何内容 - PHP

php - mysql_fetch_array 可以获取 mysql_query 作为参数吗?

php - 如何解决 PostgreSQL/PHP 中的 "Resource ID #8"错误消息?

javascript - 使用 getElementsByName 更改文本的值

c# - 如何在 C# ASP.Net 网站中通过浏览器游戏分配玩家名称?

java - 如何在 JavaFX 中设置按钮的坐标?

php - 用 PHP 显示特定的 block 大小