What are the GET and POST methods in PHP?
The GET and POST Method in PHP are the methods through which a client or a user can send information to the server.
GET and POST Methods are the HTTP
request methods used inside the <form>
tag to send the different values from the form to the server.
HTTP
is the protocol that enables the communication between the client and the server, where a browser can be the client, and an application running on a computer system that hosts your website can be the server.
The GET Method
The GET Method is a PHP super global variable “$_GET“, which is used to collect data from the form after submitting an HTML form with method=”GET”.
GET can also collect data sent in the URL.
Example:
<html>
<body>
<a href="get.php?subject=PHP&web=itsourcecode.com">Test GET</a>
</body>
</html>
When a user clicks the “Test GET” link, the parameters “subject” and “web” are sent to “get.php” file. And then a user can access their values in “get.php” file with “GET” Method.
The example below shows the code in “get.php” file:
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>
When you execute the program, this will be the output:
Study PHP at itsourcecode.com
The POST Method
In the POST method, the data from the HTML form is submitted/collected using a super global variable “$_POST“. This method sends the encoded data from the “form” tag.
Example:
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo "Your Name is: " .$name;
}
}
?>
</body>
</html>
When you execute the program this will be the output:
When you click the “submit” button, this will be the result:
What is the Difference Between GET and POST Method in PHP?
The difference Between GET and POST methods in PHP is that the GET method sends the information by appending it to the page request.
On the other hand, the POST method sends information via the HTTP header.
PHP is a server-side scripting language designed for web development. The GET and POST methods are two ways for a client computer to send information to the web server.
These methods help to retrieve information from users by forms.
Comparing GET vs POST Method in PHP
GET | POST | |
---|---|---|
BACK button/Reload | Harmless | Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted) |
Bookmarked | Can be bookmarked | Cannot be bookmarked |
Cached | Can be cached | Not cached |
Encoding type | application/x-www-form-urlencoded | application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data |
History | Parameters remain in browser history | Parameters are not saved in browser history |
Restrictions on data length | Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters) | No restrictions |
Restrictions on data type | Only ASCII characters allowed | No restrictions. Binary data is also allowed |
Security | GET is less secure compared to POST because data sent is part of the URL Never use GET when sending passwords or other sensitive information! | POST is a little safer than GET because the parameters are not stored in browser history or in web server logs |
Visibility | Data is visible to everyone in the URL | Data is not displayed in the URL |
Related Articles
- PHP Web Concepts (Explanation With Examples)
- PHP Upload File with Example
- PHP Strlen With Program Example
- PHP Unlink Function With Example Program
- PHP Read File Line By Line With Example
Conclusion
We have completely discussed what is PHP GET and POST Method and their difference. Also, we provide an example program for the two methods.
I hope this simple tutorial will help you a lot to comply with your requirements in PHP Programming.
By the way, if you have any questions or suggestions about this tutorial, Please feel free to comment below. Thank You!