Search code examples
phpgetissetis-empty

empty($_GET['variable']) blocks the script when the variable has no value


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.


Solution

  • 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";