Introduction
When building dynamic websites, developers often need to send data from a client (browser) to the server. In PHP, this is commonly done using HTTP request methods. The two most commonly used methods are:
- GET
- POST
Both methods are used to send data from a form to the server, but they work in different ways and are used in different situations.
What is the GET Method in PHP?
The GET method is used to send data to the server through the URL. The data is appended to the URL in the form of query parameters.
These parameters appear after a question mark (?) in the URL.
Example URL:
- name and age are parameters
- Their values are Rahul and 21
In PHP, GET data is accessed using the $_GET superglobal array.
Syntax of GET Method
$_GET['parameter_name']
Example of GET Method
HTML Form
<form method="get" action="process.php">
Name: <input type="text" name="name">
<input type="submit">
</form>
PHP File (process.php)
$name = $_GET['name'];
echo "Welcome " . $name;
If the user enters Rahul, the URL will look like: process.php?name=Rahul
Output
Explanation
Here:
- The form uses method=”get”
- The data is sent through the URL
- PHP retrieves the value using $_GET
Advantages of GET Method
- Simple and easy to use
- Data can be bookmarked
- URLs can be shared
- Good for search queries
Example:
example.com/search.php?keyword=php
Disadvantages of GET Method
- Data is visible in the URL
- Not secure for sensitive information
- Limited data length (URL size restriction)
- Not suitable for large data
What is the POST Method in PHP?
The POST method is used to send data to the server through the HTTP request body, not through the URL.
This means the data is not visible in the address bar.
POST is commonly used when submitting:
- Login forms
- Registration forms
- File uploads
- Payment forms
In PHP, POST data is accessed using the $_POST superglobal array.
Syntax of POST Method
$_POST['parameter_name']
Example of POST Method
HTML Form
<form method="post" action="process.php">
Name: <input type="text" name="name">
<input type="submit">
</form>
PHP File (process.php)
$name = $_POST['name'];
echo "Welcome " . $name;
If the user enters Rahul, the URL remains: process.php
Output
Explanation
- The form uses method=”post”
- Data is sent through the HTTP body
- PHP accesses the data using $_POST
Advantages of POST Method
- More secure than GET
- Data is not visible in the URL
- Can send large amounts of data
- Supports file uploads
Disadvantages of POST Method
- Data cannot be bookmarked
- Cannot easily share form data through URL
- Slightly slower than GET in some cases
Difference Between GET and POST
The GET and POST methods have several important differences.
| Feature | GET | POST |
|---|---|---|
| Data Location | URL | HTTP request body |
| Visibility | Visible in URL | Not visible |
| Security | Less secure | More secure |
| Data Size | Limited | Large data allowed |
| Bookmarking | Supported | Not supported |
| File Upload | Not supported | Supported |
Real Life Example
1. Search Form (GET)
Search forms usually use the GET method.
Example:
google.com/search?q=php
The query appears in the URL, which makes it easy to share and bookmark.
2. Login Form (POST)
Login forms should always use the POST method.
Example:
<form method="post">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit">
</form>
Using GET here would expose the password in the URL, which is not secure.
Example Combining GET and POST
if($_SERVER["REQUEST_METHOD"] == "POST"){
echo "Data received using POST method";
}
if($_SERVER["REQUEST_METHOD"] == "GET"){
echo "Data received using GET method";
}
This code checks which method is used to send the request.
Conclusion
Both GET and POST methods are essential for sending data from the client to the server in PHP applications. The GET method sends data through the URL, making it useful for search queries and shareable links. However, it is less secure and has limitations on data size.
On the other hand, the POST method sends data through the HTTP request body, which makes it more secure and suitable for sensitive information such as login credentials and form submissions.
GET vs POST method? – Objective Questions (MCQs)
Q1. GET method sends data via ______.
Q2. POST method sends data via ______.
Q3. Which method is more secure for sensitive data?
Q4. Which method has size limitations?
Q5. Which method is bookmarkable?