PHP IF语句问题与isset& !空

时间:2015-07-23 18:06:49

标签: php cookies

我今天只是陷入困惑之中,我希望有人能够提供协助:)

我有一个充满基本项目的数据库,在该表中有项目名称,项目编号,项目图像等属性。我能够毫无问题地输入新项目/显示现有项目等。

当我想编辑项目时,似乎出现了我的问题。我的想法是,我必须创建一个IF语句,以确定是否有新文件上传,如果有,则在数据库中设置新文件名,如果没有,则将旧文件保留在数据库中

我已经玩了好几天了,我觉得我开始有点太远了。我已经开始打破基础知识了,我对我的IF声明感到困惑,感觉它倒退了吗?这有意义吗?

示例:

if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(isset($OldProjectImage1)){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

现在在搜索StackExchange时,我发现我们必须在COOKIE部分上执行语句,而不是像上面那样使用Variable,但它也同样失败。

if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(!empty($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

我也尝试过isset

if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(isset($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

我已经尝试了我的脚本,他们的行为都相似。也许我对整个过程感到困惑?

当我在启用和不启用cookie的情况下运行我的测试时,似乎总是跳过IF语句的第一部分(同时使用isset和!)并跳转到下一部分。然后,类似地,感觉IF语句是向后的(如果这有意义) - 如果我设置要上传的文件填充ProjectImage1,我得到“Image1 FILES为空”。如果我没有设置文件来上传并提交表单,我会得到“Image1 FILES isset”。

我认为用简单的英语基本上是

If cookie is empty then echo "Your Browser Cookies are not enabled"
Else if ProjectImage1 Name is set, echo "Image1 FILES isset"
Else if ProjectImage1 Name is Empty, echo "Image1 FILES empty"

但我觉得它倒退了吗?我理解这个错误吗?

感谢先进的任何回复!

2 个答案:

答案 0 :(得分:1)

问题在于:

if(isset($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
}

您检查cookie是否存在,如果存在,则表示未启用cookie。有点奇怪。加!在确定之前。然后if语句和文本都是正确的。

我想,但我只能假设,你最终想要这个:

if (isset($_COOKIE["OldProjectImage1"])){
    // I believe the variable below can also be put between the else { and } down below
    $OldProjectImage1 = $_COOKIE["OldProjectImage1"];
}

if(!isset($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
} else if(!isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES is not set';
} else if(empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES is empty';
}else {
    // upload file here
}

答案 1 :(得分:0)

我认为你想检查浏览器cookie是否已启用?

  

Detect if cookies are enabled in PHP

Shiplu Mokaddim回答:

session_start();
if (isset($_GET['check']) && $_GET['check'] == true) {
    if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
        // cookie is working
        // get back to our old page
        header("location: {$_SESSION['page']}");
    } else {
        // show the message "cookie is not working"
    }
} else {
    // save the referrer in session. if cookie works we can get back to it later.
    $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
   // set a cookie to test
    setcookie('foo', 'bar', time() + 3600);
    // redirecting to the same page to check 
    header("location: {$_SERVER['PHP_SELF']}?check=true");
}

在Javascript中检测Cookie

  

Check if cookies are enabled

所以,结合你的代码和我自己的解释:

<?php 
    session_start();
    //check if a cookie test is started
    if (isset($_GET['check']) && $_GET['check'] == true) {
        //cookie test is started
        if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
            //cookie test success, go back to the previous page
            header("location: {$_SESSION['page']}");
        } else {
            //cookie test fail, echo the message and continue
            echo 'Your Browser Cookies are not enabled';
        }
    } else {
        //start cookie test if a cookie test wasn't done
        //check if a cookie test was done.
        if (!isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
            //start a cookie test if a cookie test wasn't done
            $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
            setcookie('foo', 'bar', time() + 3600);
            header("location: {$_SERVER['PHP_SELF']}?check=true");
        }
    }
    if(!isset($_COOKIE['OldProjectImage1'])){ 
        echo "OldProjectImage1 doesn't exists in cookies.";
    } else if(!isset($_FILES['ProjectImage1']['name'])){ 
        echo "Image1 FILES is not set";
    } else if(empty($_FILES['ProjectImage1']['name'])){
        echo "Image1 FILES is empty";
    }
?>