GET and POST – Webserver Requests in PHP

Spread the love

GET and POST

If we need any information from the web server or we want to submit any data to the web server using PHP, then we can achieve this task using two types of web server requests methods. Those are GET and POST.

In the web application, we can send a request to the web server using web browsers. The browser will send information to the web server using name – value pairs in a string fashion via URL. It encodes that string using scheme named URL Encoding.
The URL encoding pattern is like – emp_id1=1001&emp_id2=1002&emp_id3=1003.
Here data is combined with name and its value using an equals sign. Other data are get separated using ampersand(&) sign.

In this type of pattern generation:
If white space arrives then white space is removed and + character is added in place of space. After formation of this string, PHP completes URL encoding and send data to the web server.

 

Let’s see GET and POST requests in details.

GET Request/Method:

In GET request URL encoded data is appended to the page request URL. Encoded data and page URL is separated by a question mark (?) character. This type of URL formation is called as query string pattern. We can call URL encoded data appended after question mark as a query string.

We can also send this type of request by manually creating query string and placed in browser URL bar.

Example: http://codingkala.com/php_tutorials?emp_name1=name1&emp_name2=name2&emp_age=25

GET request have following important characteristics:

  1. GET request creates query string which is visible in browser’s URL address bar. Hence it not called as a secure request as data is visible to the end user. Hence we should never use GET method to send sensitive data like password and other critical information.
  2. GET request can only send up to 1025 characters of data in the single request.
  3. Data sent using GET method can be accessed using a $_GET associative array.
  4. GET method is not able to send binary data like files, images to the server.

Example:

In the form, we can specify GET request in method attribute.

<form action="<?php $_php_self ?>" method="GET">
<label>Employee Number</label>
<input name="emp_no" type="text" />
<label>Employee Designation</label>
<input name="emp_desig" type="text" />
</form>

<?php
if(isset($_GET) and !empty($_GET))
{
if($_GET['emp_no'] && $_GET['emp_desig'])
{
echo "Employee Number - ".$_GET['emp_no'];
echo "<br>Employee Designation- ".$_GET['emp_desig'];
}
}
?>

Output = Employee Number – 1001 //If 1001 value entered in form’s emp_no input box
Employee Designation – Software Engineer//If 25 value entered in form’s emp_desig input box.

As shown in above example, $_GET is having name-value pair. Input box names become keys of a $_GET array variable and contain their respective values. Similarly, the name of all the form inputs will become key and their values will become the value of respective array variable and we can access those values using names as a key in $_GET superglobal array.

 

POST request/method:

In POST request all the data is transferred through HTTP headers.

Important characteristics of POST request:

  1. In post requests, data is sent via HTTP headers hence data is not visible to end user. Hence POST  request is more secure than GET request.
  2. Using POST request there is no restriction on the size of data to be sent.
  3. Data sent using POST request can be accessed using $_POST superglobal array variable.
  4. We can send binary and ASCII data via POST request.

Example:

In POST request if want to send data via form then in form’s method attribute should have “POST”. If no method is specified then by default the method will be GET method.

<form action="<?php $_php_self ?>" method="POST">
<label>Employee Number</label>
<input name="emp_no" type="text" />
<label>Employee Designation</label>
<input name="emp_desig" type="text" />
</form>

<?php
if(isset($_POST) and !empty($_POST))
{
if($_POST['emp_no'] && $_POST['emp_desig'])
{
echo "Employee Number - ".$_POST['emp_no'];
echo "<br>Employee Designation- ".$_POST['emp_desig'];
}
}
?>

Output = Employee Number – 1001 //If 1001 value entered in form’s emp_no input box

Employee Designation – Software Engineer//If 25 value entered in form’s emp_desig input box.

$_REQUEST variable:

As stated earlier, we can use access both $_GET and $_POST data using $_REQUEST superglobal array variable also. We can also access data of $_COOKIES via a $_REQUEST variable. If we don’t want to use separate $_GET or $_POST variable $_REQUEST is the best alternative for this purpose, as it contains data sent by the GET and POST requests.

Example:

<form action="<?php $_php_self ?>" method="POST">
<label>Employee Number</label>
<input name="emp_no" type="text" />
<label>Employee Designation</label>
<input name="emp_desig" type="text" />
</form>

<?php
if(isset($_POST) and !empty($_POST))
{
if($_POST['emp_no'] && $_POST['emp_desig'])
{
echo "Employee Number - ".$_POST['emp_no'];
echo "<br>Employee Designation- ".$_POST['emp_desig'];
}
}
?>

Output = Employee Number – 1001 //If 1001 value entered in form’s emp_no input box

Employee Designation – Software Engineer//If 25 value entered in form’s emp_desig input box.

In the upcoming chapters, we will see the use of $_GET, $_POST and $_REQUEST in more details.


Spread the love

Leave a Comment