代码之家  ›  专栏  ›  技术社区  ›  Jeahel

当变量没有值时,empty($_GET['variable'])会阻止脚本

  •  1
  • Jeahel  · 技术社区  · 10 年前

    我正在编写一个非常简单的脚本,它使用GET参数来定义脚本中的变量。因此,在脚本的开头,我检查GET参数是否存在,并且它不是空的(为了避免 ...page.php?param= ).

    我写了这段代码(参数名为 a ) :

    if (!isset($_GET['a']) || empty($_GET['a'])) {
        header("Location: https://..."); // redirect to home page
        die();
    }
    

    它在根本没有GET参数时工作,但如果有 ?a ?a= ,即使我添加了一个 echo "some text";

    我真的不明白发生了什么事。有人能给我解释一下吗?

    谢谢:-)

    编辑:这是整个代码页:

    <?php
    
    if (!isset($_GET['a']) || trim($_GET['a']) == '' || $_GET['a'] == NULL) {
        header("Location: https://google.com");
        exit();
    }
    
    echo "hello";
    

    因此,我应该转向谷歌。com或打印“hello”,但这一切都没有发生。

    3 回复  |  直到 10 年前
        1
  •  2
  •   Martin Miguel Stevens    10 年前

    空白页面是PHP错误的典型示例。你需要设置和 use PHP error logging facility 如此:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    在页面的最顶部。

    改写你的页面我会这样做:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    if (!isset($_GET['a']) || is_null($_GET['a'])) {
        header("Location: https://google.com");
        exit();
    }
    
    echo "hello";
    
        2
  •  -2
  •   Deniz B.    10 年前

    试试这个:

    if (!isset($_GET['a']) || trim($_GET['a']) == '' || $_GET['a'] == NULL) {
        header("Location: https://www.google.com"); // redirect to home page
    }
    
        3
  •  -2
  •   Thamilhan    10 年前

    尝试

    if (!isset($_GET['a']) || trim($_GET['a']) == "") {
    

    检查手册是否为空 http://php.net/manual/en/function.empty.php