- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
Difference between GET and POST method in PHP
Two HTTP request methods are used by PHP to send data from a client (like a web browser) to a server: GET and POST. Both methods are used to transmit data, but they each have unique properties and applications.
PHP GET Method
The GET method in PHP allows data to be passed from a web page to a PHP script via the URL. The information is added to the URL as a query string when a user clicks on a link or submits a form using the GET method so that the PHP script can process it.
PHP GET Method Example
Let's build a PHP "GET" method demonstration example. In order to handle the form data, let's first create an HTML form with the action attribute set to the URL of the PHP script as follows:
<form action="process.php" method="GET"> <label for="name">Name:</label> <input type="text" name="name"> <br> <label for="email">Email:</label> <input type="email" name="email"> <br> <input type="submit" value="Submit"> </form>
This is a HTML form that allows a user to enter their name and email to submit. The form is submitting data to the "process.php" script using the GET method, which means that the form data will be appended to the URL as a query string.
The $_GET superglobal in "process.php" retrieves form data. $_GET['name'] retrieves the name value. $_GET['email'] retrieves the email value. Now is the time to create the "process.php" script to handle the form data.
<?php if (isset($_GET['name']) && isset($_GET['email'])) { $n = $_GET['name']; $e = $_GET['email']; echo "Hi <b>$n</b>, Your email address is <b>$e</b>."; } else { echo "Error: Missing parameters."; exit; } ?>
Now this PHP code checks if the 'name' and 'email' parameters are set in the URL query string using the isset() function. If both parameters are set, the code assigns their values to variables $n and $e, respectively, using the $_GET superglobal.
The code then outputs a personalized message "Hi $n, Your email address is $e." to the user, where $n is the value of the 'name' parameter and $e is the value of the 'email' parameter in the query string. The message indicates that the form data are processed successfully. If either 'name' or 'email' parameter is missing from the query string, the code will output an error message ""Error: Missing parameters." and exit the script using the exit() function. This is done to prevent the code from continuing to run if the required parameters are missing.
Now, enter "William" in the "Name" box and "wil123@gmail.com" in the "Email" box, then press the enter key. The information from the form will be sent to the "process.php" script, which will make the following output along with the information shown in the URL.
PHP POST Method
The POST method in PHP is a way to submit data to a web server from an HTML form. Unlike the GET method, which appends the form data to the URL query string, the POST method sends the data as a separate message in the HTTP request body. This makes it more secure and suitable for submitting sensitive data like passwords or credit card numbers.
PHP POST Method Example
Because the POST method is a secure way to post form data, we can use it to handle some critical forms such as signup or login. As an example:
<form action="login.php" method="POST"> <label for="user">Username:</label> <input type="text" name="user"> <br> <label for="password">Password:</label> <input type="password" name="password"> <br> <input type="submit" value="Login"> </form>
This is a HTML form that allows a user to enter their username and password to login. The POST method sends the data as a separate message in the HTTP request body, not as a query string appended to the URL. Now is the time to create the "login.php" script.
<?php if (isset($_POST['user']) && isset($_POST['password'])) { $u = $_POST['user']; $p = $_POST['password']; if (valid_login($u, $p)) { echo "Hi $u, welcome again!"; } else { echo "Invalid username or password."; } } else { echo "Error: Missing parameters."; exit; } function valid_login($user, $password) { return ($user == 'william' && $password == 'will@123'); } ?>
In this example, the script checks if the 'user' and 'password' parameters are set in the $_POST superglobal using the isset() function. If both parameters are set, the script assigns their values to variables $u and $p, respectively.
The script then calls a function valid_login() to authenticate the user and password. In this case, the function simply checks if the username is "william" and the password is "will@123". If the login is valid, the script outputs a personalized message, "Hi $u, welcome again!" to the user, where $u is the value of the 'user' parameter in the form data. If the login is invalid, the script outputs the error message "Invalid username or password."
If either the "user" or "password" parameter is missing from the form data, the script will show the error message "Error: Missing parameters" and end the script using the exit() function. This is done to stop the code from running if it doesn't have all the required parameters.
Let me show you the demo. I just typed the username and password values as shown in the following snapshot:
When I clicked on the "Login" button, here is the output I got:
Important: The preceding example is provided solely for your convenience. It is not recommended to use the above code to implement login in your application. Because other codes must be added before logging the user into the website or application to handle the form data more efficiently. I made another post with a more detailed description of the PHP and MySQLi login page.
Advantages of the GET method in PHP
- The GET method is less complicated and easier to use than the POST method.
- Because the data is included in the URL, GET requests are easily bookmarked and shared.
- Browsers and other intermediate devices can cache GET requests, improving performance and reducing server load.
- Because they do not change the server state, GET requests are appropriate for retrieving data from the server.
Disadvantages of the GET in PHP
- The data passed in GET requests has a limited size (usually around 2048 characters), which limits the amount of data that can be sent in a single request.
- If sensitive data is being transmitted, GET requests expose data in the URL, which can be a security risk.
- GET requests should not be used to send sensitive data such as passwords or credit card numbers.
Advantages of the POST method in PHP
- As the data is sent in the HTTP request body and not the URL, POST requests can send large amounts of data.
- GET requests are less secure than POST requests because the data is revealed in the URL.
- Such as submitting a form or updating a database, POST requests can be used to modify server state.
- POST requests can accommodate additional data types, including binary data and files.
Disadvantages of the POST in PHP
- POST requests are more difficult to set up and use than GET requests.
- Because the data is not included in the URL, POST requests cannot be easily bookmarked or shared.
- Browsers and other intermediate devices do not cache POST requests, which can reduce performance and increase server load.
« Previous Tutorial Next Tutorial »