🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

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.
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
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
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

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.