Request Method in PHP and Super Globals in PHP


What is HTTP?

In this tutorial, you will learn the HTTP request method in PHP. Hypertext Transfer Protocol (HTTP) is a client and server communication protocol. It enables the communication between server and client using the request-response protocol. For instance, the client sends a request to the server and the server return response that contains a status code along with request information.

HTTP Methods

The HTTP protocol allows the following methods to send requests to the server.

  • GET
  • POST
  • PUT
  • HEAD

Out of the above HTTP methods, GET and POST methods are most common and useful. In this PHP tutorial, you will learn about GET and POST methods in PHP.

HTTP GET request Method in PHP

The GET method sends data to a specific resource. Get method uses a Query string (key/value pair of data) to send data in the URL.

Properties of GET method

  • Can cache by memory.
  • Remain in the browser history.
  • We can bookmark it.
  • Not recommended when dealing with sensitive data.
  • Specific length restrictions. It can handle maximum 1024 characters.
  • Only used to request data. Cannot modify.

In PHP $_GET is a global array containing the get requests information in the form of key-value pair (associative array).

HTTP POST request Method in PHP

POST is the most useful request method in HTTP protocol. POST method sends the data sent in the request body. This method creates and updates resources.

Properties of POST method

  • Never cache request in memory.
  • Do not remain in the browser history.
  • It cannot be bookmarked.
  • There is no restrictions on data length.
  • It is more secure to send the data as compared to GET method.

In PHP $_POST is a global array containing post requests information in form of key-value pair (associative array).

HTTP PUT request Method in PHP

Both PUT and POST methods are used to Create or Update resources on the server. However, there is a difference between the POST and PUT methods. Sending POST requests, again and again, will create the same resources multiple times. On the other hand, the PUT method produces the same result upon sending requests again and again.

HEAD request Method in PHP

The HEAD method works similarly to the GET method as both are used to get resources. However, the HEAD method is different from GET method in that it does not return the resource in the response. For example, making a GET request /users will send a list of users in response. But making a HEAD request /users will return will not return a list of users.

The HEAD request is basically used to check what will be returned in the response. Such as before downloading a large file or getting a big response.

DELETE request Method in PHP

DELETE request method removes some specific resources.

OPTIONS request Method in PHP

OPTIONS method specifies the communication options for the target resource.

In the next section, we will compare the GET and POST methods as these are mostly used HTTP methods by the developers. You should know which method is suitable to use. After moving towards the advanced PHP tutorials, this content will help you to develop GET and POST form data with more understanding.

When to use GET and POST?

The GET method sends the request data as a part of the request URL. It is clear from here that, if you are on a sensitive platform where data cannot be exposed to the world, the GET method is never used. The GET method has a limit of a maximum of 2000 characters in the URL. Hence, request data over this limit is not entertained by this method.

The POST method is much more safer and secure as the data is sent using the request body. In this way, it is not visible to anyone and hence, cannot be modified. Also, there is no limit to data, POST method can be used for making both small and large size requests. As the GET method uses a URL to send data, so it can be bookmarked. But, bookmarking the POST requests is not possible.

Super Global variables in PHP

Super global variables in PHP are accessible from anywhere and store specific information in them. We have the following list of super global variables in PHP.

Variable NameDescription
$GLOBALSContains all global variables. These variables are always available at any scope.
$_REQUESTUsed to get data after submitting HTML form.
$_COOKIEContains all the cookies in the current script.
$_POSTCollect data from HTML form when method=post
$_GETCollect data from HTML form when method=get
$_FILESAssociative array of items uploaded to the current script using method=post
$_ENVEnvironment variables are available for the current script.
$_SESSIONStore session variables for the current session.
$_SERVERStore information about Header, Path, and Script locations.
Global variables in PHP

JSON stands for Javascript Object Notation. JSON is a representation of data in the form of javascript objects. It is highly readable and used as a request input and output structure. The post data can be sent in the form of JSON objects. On the other hand, the response can also be in JSON format.

One thing that makes JSON objects highly useable is that it is compatible with almost every server-side and client-side language. Owing to this, developers can make requests and get responses by using a standard JSON format.

For official reference to PHP requests click here.

Add comment

Tutor Network

Learn PHP from A to Z