dounao1875 2016-05-07 08:02
浏览 269
已采纳

empty($ _ GET ['variable'])在变量没有值时阻止脚本

I am writing quite a simple script that uses a GET parameter to define a variable in the script. So at the beginning of the script, I check that the GET parameter exists and that it's not empty (to avoid ...page.php?param=).

I wrote this piece of code (the parameter is named a) :

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

And it works when there is no GET parameter at all, but if there is either ?a or ?a=, then the page is just blank, even though I add an echo "some text";

I don't really understand what is going on. Could someone explain it to me?

Thank you :-)

EDIT : here is the whole code page:

<?php

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

echo "hello";

So I should either redirect to Google.com or print "hello" but none of this happens.

  • 写回答

3条回答 默认 最新

  • dongliuliu0385 2016-05-07 09:01
    关注

    A blank page is a classic example of a PHP error. You need to set up and use PHP error logging facility like so:

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

    At the very top of your page.

    Rewriting your page I would do this:

    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条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)