1

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.

Jeahel
  • 1,012
  • 2
  • 13
  • 30
  • Could you add the code of the page with the `echo "some text";`? – Chris May 07 '16 at 08:05
  • You need to use `&&` instead of `||` – abhishek bagul May 07 '16 at 08:09
  • Instead of `die();`, use `exit();` after `header(...);` function call. – Rajdeep Paul May 07 '16 at 08:10
  • 2
    Possible duplicate of [PHP: Utilizing exit(); or die(); after header("Location: ");](http://stackoverflow.com/questions/8665985/php-utilizing-exit-or-die-after-headerlocation) – Rajdeep Paul May 07 '16 at 08:10
  • @RajdeepPaul I replaced `die();` with `exit();`. No change in the behaviour. Also, my post is not a duplicate I think, because my question is about `empty();` rather than `die();` – Jeahel May 07 '16 at 08:27
  • @RajdeepPaul `die` and `exit` are *almost* the same thing. It doesn't matter. The differences are slight and not part of this problem. – Martin May 07 '16 at 09:06
  • *the page is just blank* means something is broken. Add these lines `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of your PHP script and see if it yields any error or not. – Rajdeep Paul May 07 '16 at 09:06
  • @Martin The header output will be fairly different for `exit()` and `die()`. *there is a distinct difference in Header output.* is what [this answer](http://stackoverflow.com/a/20932511/5517143) said. – Rajdeep Paul May 07 '16 at 09:10
  • @RajdeepPaul yes, but that answer does not relate to the issues faced in *this* question! – Martin May 07 '16 at 09:14
  • @Martin You could be right, hence I made a comment and not an answer. Glad that the issue has been resolved. *Cheers!* :-) – Rajdeep Paul May 07 '16 at 09:19

3 Answers3

2

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";
Martin
  • 22,212
  • 11
  • 70
  • 132
-2

try

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

check the manual for empty http://php.net/manual/en/function.empty.php

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
MrGapo
  • 348
  • 3
  • 10
  • The manual states that a empty string (`""`) is considered empty and should return `true`. It is what I want, so why not using `empty()`? – Jeahel May 07 '16 at 08:29
-2

Try this:

if (!isset($_GET['a']) || trim($_GET['a']) == '' || $_GET['a'] == NULL) {
    header("Location: https://www.google.com"); // redirect to home page
}
Deniz B.
  • 2,532
  • 1
  • 18
  • 35
  • That doesn't work and I don't see why `empty` is not OK. Isn't it supposed to check what you asked me to try? I also added the whole page code in my first post. – Jeahel May 07 '16 at 08:20