- 5 years ago
- Zaid Bin Khalid
- 18,086 Views
-
11
In this tutorial, we will figure out how to send data to the server utilizing HTTP GET, POST and REQUEST strategies and recover them utilizing PHP.
Strategies for Sending Information to Server
An internet browser speaks with the server with the help of HTTP and HTTPS (Hypertext Transfer Protocol) protocols. The two techniques pass the data diversely and have various points of interest and weaknesses, as depicted beneath.
The GET Method
In GET technique the information is sent as URL parameters that are generally strings of name and worth sets isolated by ampersands (and). By and large, a URL with GET information will resemble this.
http://www.example.com/action.php?name=A&age=24
https://learncodeweb.com/?s=images
In the above examples after what sign (?) parameters start in. The first value is a parameter and equals some value parameter=value and the next parameter is joined with the help of and (&) sign. One can just send basic content information by means of GET strategy.
Advantages and Disadvantages of Using the GET Method
Since the information sent by the GET technique is shown in the URL. It is conceivable to bookmark the page with explicit inquiry string esteems.
The GET strategy isn’t appropriate for passing delicate data, for example, the username and secret word, in light of the fact that these are completely noticeable in the URL question string just as conceivably put away in the customer program’s memory as a visited page.
In the $_GET method the URL is restricted you can not send a very long URL with this method.
There is a superglobal variable $_GET in PHP that use to get to all the data sent either through the URL or submitted through an HTML structure utilizing the method=”get” as shown in below snippet.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP GET method</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
echo "<p>Hi, " . $_GET["name"] . "</p>";
}
?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
The POST Method
In the POST strategy the information is sent to the server as a bundle in a different correspondence with the preparing content. Information sent through the POST technique won’t notice in the URL.
Advantages and Disadvantages of Using the POST Method
It is more secure than GET in light of the fact that client entered data is never unmistakable in the URL question string or in the server logs.
There is a lot bigger cutoff on the measure of information that can be passed and one can send content information just as paired information (transferring a record) utilizing POST.
Since the information sent by the POST technique isn’t obvious in the URL, so it is beyond the realm of imagination to bookmark the page with the explicit questions.
Like $_GET, PHP gives another superglobal variable $_POST to get to all the data sent by means of post strategy or submitted through an HTML structure utilizing the method=”post”.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP POST method</title>
</head>
<body>
<?php
if(isset($_POST["name"])){
echo "<p>Hi, " . $_POST["name"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
The $_REQUEST Variable
PHP gives another superglobal variable $_REQUEST that contains the estimations of both the $_GET and $_POST factors just as the estimations of the $_COOKIE superglobal variable. When you use this variable it will return both GET and POST values.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP $_REQUEST variable</title>
</head>
<body>
<?php
if(isset($_REQUEST["name"])){
echo "<p>Hi, " . $_REQUEST["name"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
Other superglobal variables are listed below you can use them anywhere is a PHP without defining them.
- 5 years ago
- Zaid Bin Khalid
- 18,086 Views
-
11