Kinsbane Posted November 19, 2008 Share Posted November 19, 2008 So I have a script that is meant to return only one record (it's a product view page). Right now if you try and pass in a product ID that doesn't exist, the page loads the layout structure but without all the data. This isn't usable to me so I want to throw a 404 error if they try and pass an invalid ID number. Problem is, in Firebug when I deliberately load the product viewing page with a false ID, it gives back header code 200 OK. I would like search engines to see 404 error pages if they come across invalid product / document ID's so they can be removed from their indexes. I understand some of this would need to edit Apache's config file, but I'm unsure what needs to be edited. Any help is greatly appreciated. Here's the code I'm currently using: $read_product="SELECT * FROM products WHERE control = '".$id."'"; $product = mysql_query($read_product); if (mysql_num_rows($product) < 1) { header("HTTP/1.0 404 Not Found"); include("/home/beta/public_html/404.php"); exit; } else { $data = mysql_fetch_array($product); } Quote Link to comment https://forums.phpfreaks.com/topic/133386-solved-sending-404-error-header-if-mysql-query-doesnt-return-results/ Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 The above should work without any modification...why it is not sending the error I do not know. Are you sure there is no output before the header tag is called? Quote Link to comment https://forums.phpfreaks.com/topic/133386-solved-sending-404-error-header-if-mysql-query-doesnt-return-results/#findComment-693746 Share on other sites More sharing options...
Kinsbane Posted November 19, 2008 Author Share Posted November 19, 2008 The above should work without any modification...why it is not sending the error I do not know. Are you sure there is no output before the header tag is called? Yeah, there's no whitespace outside of the PHP tags - which as I understood it would cause a header error. All the stuff in the PHP tags just format data to send to the HTML flow. The very first part of the PHP code just grabs the ID from the URL and queries the DB. Quote Link to comment https://forums.phpfreaks.com/topic/133386-solved-sending-404-error-header-if-mysql-query-doesnt-return-results/#findComment-693757 Share on other sites More sharing options...
Kinsbane Posted November 19, 2008 Author Share Posted November 19, 2008 My errors turned out to be not related to headers, but to doubly-declared functions. All is well! Quote Link to comment https://forums.phpfreaks.com/topic/133386-solved-sending-404-error-header-if-mysql-query-doesnt-return-results/#findComment-693758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.