InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Sending a Specific HTTP Status Code in PHP
PHP

Sending a Specific HTTP Status Code in PHP

InfinityCoder November 26, 2016

You want to explicitly set the HTTP status code. For example, you want to indicate that the user is unauthorized to view the page or the page is not found.

Use http_response_code() to set the response:

1
http_response_code(401);

Your web server returns HTTP status code 200 (OK) for most pages processed by PHP. But there are a wide range of status codes, or response codes, that you may need to use.

But there’s always 304 (Not Modified) for conditional GETs, when you should only return content if it’s changed since the last request.

This can be used when someone is polling your site and you want to tell them there’s nothing new to retrieve.
Or, the infamous 404 (Not Found), when a page isn’t there. Normally, this is handled by your web server. But if you want to support dynamic URLs, where there aren’t any physical files stored on disk, but you process the URL and respond to it based on information in a database, then you need to handle this yourself when someone tries to
fetch an invalid URL.
One great example is WordPress, which responds to URLs based on categories or dates (e.g., /category/php/ or /2014/11/03/).

In these cases, whenever someone adds a category or a post on a new date, WordPress can be configured to automatically respond to requests at URLs that match that pattern, even though there aren’t actually files at
that location.
With http_response_code(), you provide the status code number and PHP takes care of setting the proper Status Line.

For some status codes, including 204 (No Content), the HTTP specification states you must not provide a message body.

In these cases, it’s best to send exit() to immediately end the script. This prevents content from being
accidentally added later on:

1
2
http_response_code(204);
exit();

If you’re stuck on PHP 5.3, use header(), and pass in the status code as the third parameter:

1
header('HTTP/1.0 204 No Content', true, 204);

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Installing PECL Packages in PHP
You want to install a PECL package; this builds a …

Installing PECL Packages in PHP

Reading and Writing Compressed Files in PHP
You want to read or write compressed files. Use the …

Reading and Writing Compressed Files in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap