Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make php page return a 503 error (or anything non-200) [duplicate]

Tags:

http

php

A former developer wrote or client-server api in PHP. It simply sends messages as xml using post/response in a very simplistic fashion. The problem is that even when there is an error (ex: invalid arguments passed into the server side) we get a HTTP 200 response with a page like this

<h4>Unknown error!</h4> 

In firebug I can see that the actually HTTP response is a 200. How can we send a different response (ie:503) when we programatically detect in our php code that it is appropriate to do so.

like image 610
benstpierre Avatar asked May 03 '10 20:05

benstpierre


People also ask

How do you trigger a 503?

In the majority of cases, the 503 is triggered when the website in question is no longer able to connect with its supported server, meaning that any information requested or issued by your browser is simply hitting a wall.

Why does it show 503 Service Unavailable?

The HyperText Transfer Protocol (HTTP) 503 Service Unavailable server error response code indicates that the server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded.

What is a 503 redirect?

The original definition of the 503 status code, according to this RFC, is: The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay.

What is 503 error in REST API?

The HTTP status code 503 means that the server is currently unable to handle the incoming requests. Usually, this error occurs because the server is too busy or is temporarily down for maintenance. Possible causes for the 503 Service Unavailable response are: Cause.


2 Answers

Use PHP's header function to send the code (along with the HTTP version and any other headers you need). More complete info:

When to send HTTP status code?

http://php.net/manual/en/function.header.php

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 300');//300 seconds 
like image 171
3 revs, 2 users 71% Avatar answered Sep 19 '22 17:09

3 revs, 2 users 71%


I worked on a site that had been hacked and had to use HTACCESS to do this.

<IfModule mod_rewrite.c>   RewriteEngine on   # let this (iescaped) IP address see the real site:  # RewriteCond %{REMOTE_ADDR} !^123.45.67.89   RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]   RewriteCond %{REQUEST_URI} !.(jpe?g?|png|gif|css|js) [NC]   RewriteRule .* /maintenance.php [R=503,L]  </IfModule> 
like image 42
Ben Racicot Avatar answered Sep 17 '22 17:09

Ben Racicot