【问题标题】:PHP if statement and redirect [duplicate]PHP if语句和重定向[重复]
【发布时间】:2021-11-09 16:20:48
【问题描述】:

我正在尝试检查是否存在来自 URL 的变量。如果变量不存在,我想将用户重定向到另一个页面。如果存在,我想继续执行该页面。

我无法获得判断为假的条件。


<?php 
$exist = isset($_GET["name"]); // isset() checks to see if $_get.name var exists
echo "exist outside: $exist";
var_dump($exist);
if ($exist != 1) // if var doesn't exist
{
        
    //header ("LOCATION: list.php");
    echo "exist inside: $exist";
    
    //echo '<meta http-equiv="refresh" content="0; URL=list.php">';
}   
?>

【问题讨论】:

    标签: php if-statement redirect


    【解决方案1】:
    if(isset($_GET["name"]) === false) {
        header("location: list.php");
        exit;
    }
    

    【讨论】:

    • 成功了!!谢谢你,杜马!所以我看起来我需要使用比较运算符(===)并且我不需要将“isset($_GET[“name”]”放入另一个变量中。
    • 这与您的代码 sn-p 之间的唯一区别是您将 $exist 视为一个数字,但它是一个布尔值。
    • 实际上,这并没有什么不同,请参见这个例子,其中 1 等于 true。 onecompiler.com/php/3xgxgwajd。我认为使用标头位置时标头已发送错误出现问题。无论如何,工作示例使代码更具可读性和正确性。
    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多