Debug School

rakesh kumar
rakesh kumar

Posted on

Php Question

Difference between Get and Post.
Difference between Include and Require.
Difference between Cookie and PHP Session.
Difference between require_once and include_once.
Differene b/w die and exit

Difference between Get and Post.
Image description
Image description

GET and POST

Two common methods for the request-response between a server and client are:

GET- It requests the data from a specified resource
POST- It submits the processed data to a specified resource
Anatomy of Get Request
The query string (name/value pairs) is sent inside the URL of a GET request:

GET/RegisterDao.jsp?name1=value1&name2=value2

As we know that data is sent in request header in case of get request. It is the default request type. Let's see what information is sent to the server.

Image description
Some other features of GET requests are:

It remains in the browser history
It can be bookmarked
It can be cached
It have length restrictions
It should never be used when dealing with sensitive data
It should only be used for retrieving the data
Enter fullscreen mode Exit fullscreen mode

Anatomy of Post Request
The query string (name/value pairs) is sent in HTTP message body for a POST request:

POST/RegisterDao.jsp HTTP/1.1

Host: www. javatpoint.com

name1=value1&name2=value2

As we know, in case of post request original data is sent in message body. Let's see how information is passed to the server in case of post request.

Image description
Some other features of POST requests are:

This requests cannot be bookmarked
This requests have no restrictions on length of data
This requests are never cached
This requests do not retain in the browser history
Enter fullscreen mode Exit fullscreen mode

KEY DIFFERENCE:
In GET method, values are visible in the URL while in POST method, values are NOT visible in the URL.
GET has a limitation on the length of the values, generally 255 characters whereas POST has no limitation on the length of the values since they are submitted via the body of HTTP.
GET method supports only string data types while POST method supports different data types, such as string, numeric, binary, etc.
GET request is often cacheable while POST request is hardly cacheable.
GET performs are better compared to POST.

PHP Get Form
Get request is the default form request. The data passed through get request is visible on the URL browser so it is not secured. You can send limited amount of data through get request.

Let's see a simple example to receive data from get request in PHP.

File: form1.html

<form action="welcome.php" method="get">  
Name: <input type="text" name="name"/>  
<input type="submit" value="visit"/>  
</form>  
Enter fullscreen mode Exit fullscreen mode

File: welcome.php

<?php  
$name=$_GET["name"];//receiving name field value in $name variable  
echo "Welcome, $name";  
?>  
Enter fullscreen mode Exit fullscreen mode

PHP Post Form
Post request is widely used to submit form that have large amount of data such as file upload, image upload, login form, registration form etc.

The data passed through post request is not visible on the URL browser so it is secured. You can send large amount of data through post request.

Let's see a simple example to receive data from post request in PHP.

File: form1.html

<form action="login.php" method="post">   
<table>   
<tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>  
<tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>   
<tr><td colspan="2"><input type="submit" value="login"/>  </td></tr>  
</table>  
</form> 
Enter fullscreen mode Exit fullscreen mode

File: login.php

<?php  
$name=$_POST["name"];//receiving name field value in $name variable  
$password=$_POST["password"];//receiving password field value in $password variable  

echo "Welcome: $name, your password is: $password";  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:
Image description

Difference between Include and Require.

Top comments (0)