Forum Moderators: coopster

Message Too Old, No Replies

if emtpy($id) return errordocument 404

If the ID doesn't exist redirect to a 404 page.

         

scraptoft

11:59 am on Jan 24, 2007 (gmt 0)

10+ Year Member



Hi,

When a page is called i.e example.com/page?id=1 I run a select on the MYSQL database:

SELECT id FROM categories WHERE id = '$id'

If $id doesn't exist I want to direct to a 404 page instead of just using the echo as I have now:

if (empty($id) {
echo 'error no ID - I sure wish you were seeing a 404 page here instead.';
exit;

My .htaccess has the error document line and works fine, I just can't figure out what code to use instead of echo.

Could anyone help me out?

eelixduppy

12:11 pm on Jan 24, 2007 (gmt 0)



You'd use header:

$result = mysql_query($query);
if(mysql_num_rows($result) == 0) {
[url=http://us3.php.net/header]header[/url]("HTTP/1.0 404 Not Found");
}

Also, make sure to escape your query variables:


$id = [url=http://us3.php.net/manual/en/function.mysql-real-escape-string.php]mysql_real_escape_string[/url]($id);

mcibor

12:13 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



header ("HTTP/1.0 404 Not Found");
include('/path/to/404.php');
exit();

this should work
only there cannot be any headers sent before, or use Output buffering [de3.php.net]

Hope this helps you.
Michal

2 minutes lag ;)

scraptoft

1:01 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



Thank you both for your lightning responces.

I just have one question - what is the reason for including the path to 404 ( i.e include('path/to/404.php'); ) when I have already specified it in the .htaccess.

Cheers.

mcibor

1:19 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then I don't think there would be a need to include that file.
the header will take you to the default settings, which are in .htaccess

I copied/pasted it from some site found on google.

Regards
Michal

whoisgregg

2:52 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you've set up a custom 404 page in .htaccess, it will not be called automatically when PHP sends a 404 header. By the time the PHP script has been called, .htaccess has already "had it's turn" and is no longer a factor... So you will still need to use that
include
to display your custom 404 page.

I include the

header ("HTTP/1.0 404 Not Found");
in my custom 404 page so I don't have to remember that bit whenever I am returning a 404 to the visitor. :)

scraptoft

7:30 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



Thank you whoisgregg for the information and the tip - I have made sense of that now.

WW, your help has been much appreciated so far.

Now I am trying to figure out which IF statement is most practical in this situation.

if (empty($ID)){

if(mysql_num_rows($row) == 0) {

Both work fine - is there any difference besides them being different languages? Is it just preference?

Cheers.

(edit: $t was written instead of $ID - too many hours infront of the PC I guess.)

[edited by: scraptoft at 7:48 pm (utc) on Jan. 24, 2007]

eelixduppy

8:38 pm on Jan 25, 2007 (gmt 0)



This:


if (empty($ID)){

And this:


if(mysql_num_rows($row) == 0) {

Are actually doing two different things. The first one is checking to see whether or not $_GET['id'] is set in the URI query.

The second is actually running the $_GET['id'] variable through a query to see if it exists in the database table. If I was you, I'd have both. This way it will give a NOT FOUND for an empty id value and if the id is defined, but just doesn't exist in the table.

Good luck :)