0

Im trying to redirect the user so if they enter an invalid GET['id'] in the url they will be redirected to the home page.

NOTE: Ive made it work when the GET is empty with the code below

} else if (empty($_GET['id'])) {
      header("Location: home.php");
      exit;
}

But my URL is only taking ints as 'id' :

website/edit.php?id=10

What I want is to be redirected to the homepage if user entered a letter for example:

website/edit.php?id=g

Is this possible? Thanks for your time!

SalLuBeBo
  • 1
  • 2
  • 1
    Use the `!empty` and https://www.php.net/manual/en/function.ctype-digit.php, https://www.php.net/manual/en/function.is-numeric.php, or `preg_match` with your own regex. – user3783243 Oct 09 '20 at 01:52

1 Answers1

1

If the user does not submit the id parameter in the url, empty($_GET['id']) will return false because it cannot find $_GET['id'] and not because $_GET['id'] is empty.

If you want to improve your accuracy, I suggest you use the isset function instead of empty. This can also avoid a prompt like "Notice: Undefined index: id in abc.php in line 1" in your debug environment.

} else if (isset($_GET['id']) && is_int($_GET['id'])) {
      header("Location: home.php");
      exit;
}

Even so, you can just use is_int($_GET['id']) if you can be sure that there is no system message prompt in you production environment.

} else if (is_int($_GET['id'])) {
      header("Location: home.php");
      exit;
}
Wyman Liu
  • 36
  • 1
  • 5