Code has been added to clipboard!

Understand PHP HTTP Response: Functions and Constants

Reading time 2 min
Published Aug 8, 2017
Updated Oct 2, 2019

HTTP stands for Hypertext Transfer Protocol. It is called a protocol of request-response type. That means it functions by the server answering various requests it receives from the client (in this case, the Internet browser).

In this tutorial, we will explain in more detail what a PHP HTTP response is, how to set and modify it. Such a response can be modified in numerous ways, but to do that, a developer must have some knowledge on special functions.

PHP HTTP Response: Main Tips

  • HTTP PHP functions allow you to manipulate PHP HTTP response (the information sent by your browser to server) before outputting it.
  • These functions are inbuilt into PHP core.
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

List of Functions to Manipulate Responses

As we have already covered, exchanging data on the Internet consists of one side (client) asking for specific data and the other (server) supplying the requested information. A piece of data that server issues as an answer to a particular request is called a PHP HTTP response.

Response has the same structure as request. It consists of a status line, header and body. To set a certain PHP HTTP response, you should use http_response_code() function.

Now, there are various ways to control these responses. Let's see them in a table below:

Function Description
header() Send raw HTTP header to client
headers_list() Return list of PHP HTTP response headers already sent (or headers that are ready to be sent)
headers_sent() Check if / where HTTP headers have been sent
setcookie() Set cookie to send along with remaining HTTP headers
setrawcookie() Set cookie (without URL encoding) to send along with remaining HTTP headers

PHP HTTP Response: Summary

  • When a client sends a request for certain information to the server, it provides it by sending a PHP HTTP response.
  • Before HTTP data that your browser sends to the server is outputted, you can modify it using HTTP PHP functions.
  • There's no need to install these functions separately.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 18/01/2021
3176 People Used
Only 97 Left
Rating
5.0