/    Sign up×
Community /Pin to ProfileBookmark

How to get 404 for non-existent content?

Hi guys ,

My code is :

<?
session_start();

include (“lib/dbconnect.php”);

$titluID=intval($_GET[“id”]);
$titluDS=safe_query(“SELECT titluri.*, domenii.domeniu, subdomenii.subdomeniu, edituri.editura, edituri.discount as discount_editura FROM ((titluri LEFT JOIN domenii ON titluri.domeniu_id = domenii.id) LEFT JOIN edituri ON titluri.editura_id = edituri.id) LEFT JOIN subdomenii ON titluri.subdomeniu_id = subdomenii.id WHERE titluri.activ<>0 AND edituri.activa<>0 AND titluri.id = “.$titluID);
$titluRow=mysql_fetch_object($titluDS);
$detalii=$titluRow->detalii;
$titlu=$titluRow->titlu;
$autoriRS=safe_query(“SELECT autori_titluri.autor_id, autori.autor FROM autori_titluri LEFT JOIN autori ON autori_titluri.autor_id = autori.id WHERE autori_titluri.titlu_id = “.$titluID);
$autorRow=mysql_fetch_object($autoriRS);
$autorN=$autorRow->autor;
$id_titlu=$titluID;
include (“headert.php”);

?>

I should change the code , if an [B]id[/B] record is not found on my database , the answer should be an 404 error code . How can i do this ?
All Pages are like /titlu.php?id=XXXX

Thanks

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@gvreSep 07.2011 — Create a 404.html page and use the following code (not tested). When an id record is not found, call PageNotFound() function.

[code=php]function PageNotFound()
{
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
header("Location: /404.html");
exit;
}[/code]
Copy linkTweet thisAlerts:
@colonelu81authorSep 07.2011 — Thanks , i already have a 404 custom page made . But how do i check if an id record is not found ?
Copy linkTweet thisAlerts:
@gvreSep 07.2011 — From mysql_fetch_object manual page
Returns an object with string properties that correspond to the fetched row, or FALSE if there are no more rows.[/QUOTE]
So, you need to check the value of $autorRow
[code=php]if ($autorRow === false)
PageNotFound();[/code]
Copy linkTweet thisAlerts:
@colonelu81authorSep 07.2011 — thanks !
Copy linkTweet thisAlerts:
@colonelu81authorSep 08.2011 — Problem is i get a 302 code , not a 404 one

And i don't know if it's a good thing to have 302 code instead of 404 for pages which were deleted
×

Success!

Help @colonelu81 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...