<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Get or Set the HTTP response code</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.headers-sent.html">headers_sent</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.inet-ntop.html">inet_ntop</a></div> <div class="up"><a href="ref.network.html">Network Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.http-response-code" class="refentry"> <div class="refnamediv"> <h1 class="refname">http_response_code</h1> <p class="verinfo">(PHP 5 >= 5.4.0, PHP 7)</p><p class="refpurpose"><span class="refname">http_response_code</span> — <span class="dc-title">Get or Set the HTTP response code</span></p> </div> <div class="refsect1 description" id="refsect1-function.http-response-code-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>http_response_code</strong></span> ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$response_code</code></span> ] )</div> <p class="para rdfs-comment"> Gets or sets the HTTP response status code. </p> </div> <div class="refsect1 parameters" id="refsect1-function.http-response-code-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">response_code</code></dt> <dd> <p class="para"> The optional <code class="parameter">response_code</code> will set the response code. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.http-response-code-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> If <code class="parameter">response_code</code> is provided, then the previous status code will be returned. If <code class="parameter">response_code</code> is not provided, then the current status code will be returned. Both of these values will default to a <em>200</em> status code if used in a web server environment. </p> <p class="para"> <strong><code>FALSE</code></strong> will be returned if <code class="parameter">response_code</code> is not provided and it is not invoked in a web server environment (such as from a CLI application). <strong><code>TRUE</code></strong> will be returned if <code class="parameter">response_code</code> is provided and it is not invoked in a web server environment (but only when no previous response status has been set). </p> </div> <div class="refsect1 examples" id="refsect1-function.http-response-code-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5624"> <p><strong>Example #1 Using <span class="function"><strong>http_response_code()</strong></span> in a web server environment</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Get the current response code and set a new one<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">(</span><span style="color: #0000BB">404</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Get the new response code<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> int(200) int(404) </pre></div> </div> </div> <div class="example" id="example-5625"> <p><strong>Example #2 Using <span class="function"><strong>http_response_code()</strong></span> in a CLI environment</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Get the current default response code<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">());<br /><br /></span><span style="color: #FF8000">// Set a response code<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">(</span><span style="color: #0000BB">201</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Get the new response code<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> bool(false) bool(true) int(201) </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.http-response-code-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.header.html" class="function" rel="rdfs-seeAlso">header()</a> - Send a raw HTTP header</span></li> <li class="member"><span class="function"><a href="function.headers-list.html" class="function" rel="rdfs-seeAlso">headers_list()</a> - Returns a list of response headers sent (or ready to send)</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.headers-sent.html">headers_sent</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.inet-ntop.html">inet_ntop</a></div> <div class="up"><a href="ref.network.html">Network Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>