Php Return Http Error 404
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more
Php Return 404 Error
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users how to create 404 error page in php Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping
Php Force 404 Error
each other. Join them; it only takes a minute: Sign up How can I create an error 404 in PHP? up vote 26 down vote favorite 6 My .htaccess redirects all requests to /word_here to /page.php?name=word_here. The PHP how to redirect 404 error page in php script then checks if the requested page is in its array of pages. If not, how can I simulate an error 404? I tried this, but it didn't work: header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); Am I right in thinking that it's wrong to redirect to my error 404 page? php redirect http-status-code-404 share|improve this question edited May 9 '10 at 21:27 Gumbo 396k64548668 asked Sep 4 '09 at 19:29 Eric 51.4k24114226 Why do you mean it php 404 redirect htaccess doesn’t work? Did you check the server response header? –Gumbo Sep 4 '09 at 19:32 1 In answer to your concluding question, yes, it would be a bad idea to redirect all 404s to a real page. This violates the HTTP spec by turning something that shouldn't be there into something that is there. –Lucas Oman Sep 4 '09 at 19:41 add a comment| 4 Answers 4 active oldest votes up vote 50 down vote accepted What you're doing will work, and the browser will receive a 404 code. What it won't do is display the "not found" page that you might be expecting, e.g.: Not Found The requested URL /test.php was not found on this server. That's because the web server doesn't send that page when PHP returns a 404 code (at least Apache doesn't). PHP is responsible for sending all its own output. So if you want a similar page, you'll have to send the HTML yourself, e.g.: You could configure Apache to use the same page for its own 404 messages, by putting this in httpd.conf: ErrorDocument 404 /notFound.php share|improve this answer edited Mar 23 at 14:47 Kzqai 11.5k1678112 answered Sep 4 '09 at 19:50 JW. 28.8k207999 Thanks. I'd assumed it used my 404 page. –Eric Sep 4 '09 at 20:17 @
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About
Php Header 400
Us Learn more about Stack Overflow the company Business Learn more about hiring how to handle 404 error in php developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Header("http/1.0 404 Not Found");
Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHP: How to send HTTP http://stackoverflow.com/questions/1381123/how-can-i-create-an-error-404-in-php response code? up vote 103 down vote favorite 45 I have a PHP script that needs to make responses with HTTP response codes (status-codes), like HTTP 200 OK, or some 4XX or 5XX code. How can I do this in PHP? php http-response-codes share|improve this question edited Dec 25 '13 at 12:53 hakre 134k26216390 asked Jul 15 '10 at 18:23 Paulo Coghi 2,55133759 add a comment| http://stackoverflow.com/questions/3258634/php-how-to-send-http-response-code 6 Answers 6 active oldest votes up vote 219 down vote accepted I just found this question and thought it needs a more comprehensive answer: As of PHP 5.4 there are three methods to accomplish this: Assembling the response code on your own (PHP >= 4.0) The header() function has a special use-case that detects a HTTP response line and lets you replace that with a custom one header("HTTP/1.1 200 OK"); However, this requires special treatment for (Fast)CGI PHP: $sapi_type = php_sapi_name(); if (substr($sapi_type, 0, 3) == 'cgi') header("Status: 404 Not Found"); else header("HTTP/1.1 404 Not Found"); Note: According to the HTTP RFC, the reason phrase can be any custom string (that conforms to the standard), but for the sake of client compatibility I do not recommend putting a random string there. Note: php_sapi_name() requires PHP 4.0.1 3rd argument to header function (PHP >= 4.3) There are obviously a few problems when using that first variant. The biggest of which I think is that it is partly parsed by PHP or the web server and poorly documented. Since 4.3, the header function has a 3rd argument that lets you set the response code somewhat comfortably, but
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring http://stackoverflow.com/questions/4797274/how-to-send-a-status-code-in-php-without-maintaining-an-array-of-status-names developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question https://www.sitepoint.com/community/t/php-force-404-error/4490 x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to send a status code in PHP, without maintaining an array of status names? up vote 28 down vote favorite 7 All I want to do, is send a 404 status 404 error code from PHP - but in a generic fashion. Both Router::statusCode(404) and Router::statusCode(403) should work, as well as any other valid HTTP status code. I do know, that you can specify a status code as third parameter to header. Sadly this only works if you specify a string. Thus calling header('', false, 404) does not work. Furthermore I know, that one can send a status code via a header call with a status line: header('HTTP/1.1 404 Not Found') But 404 error page to do this I have to maintain an array of reason phrases (Not Found) for all status codes (404). I don't like the idea of this, as it somehow is a duplication of what PHP already does itself (for the third header parameter). So, my question is: Is there any simple and clean way to send a status code in PHP? php header http-status-codes share|improve this question asked Jan 25 '11 at 18:15 NikiC 65.9k21149200 2 The simple and clean way is to send the proper header. Any other way (including using the 3rd parameter to header() IMHO) is dirty. There are not that many status codes, and they are well documented. What's not clean about doing a simple map? –ircmaxell Jan 25 '11 at 18:20 add a comment| 4 Answers 4 active oldest votes up vote 69 down vote accepted There is a new function for this in PHP >= 5.4.0 http_response_code Simply do http_response_code(404). If you have a lower PHP version try header(' ', true, 404); (note the whitespace in the string). If you want to set the reason phrase as well try: header('HTTP/ 433 Reason Phrase As You Wish'); share|improve this answer edited Mar 8 '13 at 14:13 hakre 134k26216390 answered Jun 17 '12 at 9:50 hectorct 2,52911328 +1 for the whitespace at the beginning. The second parameter can be either true or false according to my experience. –hak
that will work. get httpfox for firefox and you can check the return codes. Zaggs 2008-12-04 21:51:53 UTC #3 galen said: that will work. get httpfox for firefox and you can check the return codes. Ok, but it's not forcing a redirect to the 404 page, any ideas? Zaggs 2008-12-05 10:57:38 UTC #4 still struggling with this? would greatly appreciate any help... Stormrider 2008-12-05 11:02:32 UTC #5 All it does its return the status code, it doesn't force a redirect to any 404 page or anything. You have to do that yourself. Zaggs 2008-12-05 11:09:48 UTC #6 Stormrider said: All it does its return the status code, it doesn't force a redirect to any 404 page or anything. You have to do that yourself. How would I redirect to 404.shtml? Stormrider 2008-12-05 11:20:41 UTC #7 Best way is probably just to include() it after sending the status code, then exit(). galen 2008-12-05 16:12:18 UTC #8 try ErrorDocument 404 /notfound.html in .htaccess Stormrider 2008-12-05 18:00:47 UTC #9 That will work for apache generated 404 errors. For ones you throw in php, you will need to output the page yourself. felgall 2008-12-05 18:30:39 UTC #10 The easiest way would be: header("Location: dkjlflkjxfgkdnbcxfn.html"); As long as you don't actually have a page with that filename. Stormrider 2008-12-05 22:45:49 UTC #11 Then there is little point in outputting the 404 header, since that would generate the error anyway. Would look a bit rubbish though. imaginethis 2008-12-05 23:09:43 UTC #12 Did you try header("HTTP/1.0 404 Not Found"); echo "