PHP Tutorial #18 ( GET and POST ) Methods and $_SERVER Variables

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

I will learn how to send and get informations using GET and POST , and how to get some properties using $_SERVER variables.       

  • How to transfer the value of the input explictly using GET method
  • How to transfer the value of the input implicitly using POST method
  • $_SERVER['PHP_SELF'], $_SERVER['REQUEST_METHOD'] and $_SERVER['QUERY_STRING']

Requirements

System Requirements:
  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  •  Browser (Chrome for example)  
OS Support for PHP
  • Windows
  • macOS
  • Linux

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  • Basic

Description

In this tutorial we will learn how the client transfer the informations to the server and how the server receive these informations using the channel " GET and POST ".

What's the server ?

A program is called a server program that provides a service in the network. The server accepts requests, processes them, and sends them back to the client.

What's the client ?

A program is called a client program that uses the services offered by a server. The client sends a request to the server and receives the response.

There are several cases that we can use the client / server architecture in PHP, a very good example, a login page a client wants to connect he will enter the username and password and the system will check the existence of this client , how it works ?

We have a form with inputs username and password, the client will fill the fields and after going click on connect, by the methods " POST or GET ' the information will be transmitted to the server as a request.

<form action="page2.php" method="GET">

<label>username:</label>

<input type="text" name="username"><br>

<label>passowrd: </label>

<input type="password" name="password"><br>

<input type="submit" value="Connect">

</form>

The server will receive an array either " GET or POST " with the values that the client has filled, the server can for example connect with the database or make conditions ..etc.

And now how do they work?

1- GET

The GET method circulates the information of the form in clear in the address bar by following the format

 www.yoursite.com?parameter1=value1&parameter2=value2 

The format is the interrogation point , then the name of the parameter and the value , between the parameters the URL gives the sign '&'. 

Contrary to what can be read frequently on the web, the maximum limit of a URL is not 255 characters. There is in fact no standard limit, this is because the maximum size of a URL can be configured on both the server and client side. 

A web server administrator can either increase or decrease the maximum length of URLs, As for browsers they also set default maximum size. It is therefore recommended not to abuse the length of a URL when you do not control the entire production environment (web server and clients).

The server will receive an associative array $_GET contains the names of inputs as keys and values as values, you must use $_GET['username'] to get the value of the key 'username', you can also use the isset() method to check the availability of the value in the array by passing the $_GET['username'] as parameter to it.

And this is the result

2- POST 

The POST method for its part, transmits the information of the form in a hidden but unencrypted manner. Failure to display the data does not mean that it is encrypted. 

Remember moreover, that this information uses the HTTP and not HTTPS protocol that encrypts the data. 

The POST method is preferred when there is a large amount of data to transmit or when sensitive data needs to be sent as passwords. 

In some cases  only the POST method is required a file upload for example.  

The server will receive an associative array $_POST contains the names of inputs as keys and values as values, you must use $_POST['username'] to get the value of the key 'username', you can also use the isset() method to check the availability of the value in the array by passing the $_POST['username'] as parameter to it.

And this is the result

3 - PHP_SELF

By using the php_self you can find the file name of the currently executing script with the backslach before the name , we can use it in the action attribute.

4 - REQUEST_METHOD 

The request_method get the request or the query passed on the URL , it can be a post,get, head ..etc requests.

5- QUERY_STRING 

If a page is accessed via any query string, this variable will return the query_string, we can pass it to the parse_str method to get the parameters.  

I will use all of them in the same page to understand when they can be used 

I have used the PHP_SELF to stay in the same page , and REQUEST_METHOD to get the method is GET or POST and the QUERY_STRING to get the data passed by the address bar and this is the result

Video Tutorial  

Sort:  

Hi @alexendre-maxim

Thank you for contributing to the video tutorial category.

I can see that your presentation has improved. Your points are getting more and more clear.

The concepts are well explained also.

The delivery is also at good pace.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

@rosatravels Thank you , I am very happy with this.

Thank you for your review, @rosatravels! Keep up the good work!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.31
TRX 0.11
JST 0.034
BTC 64549.55
ETH 3170.62
USDT 1.00
SBD 4.13