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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘