0

Need assistance with the following code, i have a form providing a "id", when submitted the next script gets the data from the database, if for some reason the "id" is zero, how can i forward the url to my 404 page.

Code:

$id=$_GET['id'];
        include ('dbconnection.php');
        include ('dbopen.php');
        include ('header.php'); 

I have tried the following, but no success to move client to the 404.php page if "id" value is null.

if (isset($_GET['id']) && !empty($_GET['id'])) {

  header('Location: index.php');
}

Please help :D

Boldie
  • 25
  • 1
  • 12

2 Answers2

4

Use this:

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('Location: 404.php');
    exit(); // don't execute any code after it!
}
noob
  • 8,982
  • 4
  • 37
  • 65
  • That generates "soft 404" which is going to cause problems for crawlers. When something isn't found you should use `header('HTTP/1.1 404 not found');` and display error without redirecting. – Kornel Dec 18 '11 at 15:34
  • I know but he asked for this and I think the current protocol is `HTTP/1.0` – noob Dec 18 '11 at 16:31
  • It doesn't really matter whether you output HTTP/1.0 or 1.1 — servers will deal with the details for you. Status you output is the key difference. – Kornel Dec 18 '11 at 16:40
1

Your code should work. But there shouldn't be any output (echo or content out of ) before header.

Ximik
  • 2,435
  • 3
  • 27
  • 53