PHP POST and GET Method

PHP POST and GET Method are used to retrive data from another page or same page which are sent by FORM. Data sent by post request can be retrived by $_POST variable. Data sent by get request can be retrived by $_GET variable.

There are two methods to get data sent by form.
1. POST method
2. GET Method

PHP POST Method

Form POST Method sends data as hidden. Data sent by POST Method can not be visible in web browser.

Data sent by POST Method is more secure compare to GET Method.

POST Method can send large amount of data like file upload, image upload, login form, registration form etc.

We should use post method for login form because username and password details will not be passed in web browser address bar.

$_POST variable is used to receive data from post request in PHP.

Also $_REQUEST variable is used to receive data from post request in PHP.

PHP Example :

Enter your username and password in below form.

File : login.php

<form action="thanks.php" method="post">   
    <table>   
        <tr><td>Username:</td><td> <input type="text" name="username"/></td></tr>  
        <tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>   
        <tr><td></td><td><input type="submit" name="submit"  value="login"/>  </td></tr>  
    </table>  
</form>  

File : thanks.php

<?php  
    $username=$_POST["username"]; //receiving username field value by $_POST variable
    $password=$_POST["password"]; //receiving password field value by $_POST variable
     echo "Welcome, $username. your password is $password";  
 ?> 

Output:

Welcome, aryatechno. your password is learnphp

 

PHP GET Method

Data sent by GET method is visible in web browser address bar.

GET method is not secure compare to POST Method because data is paased through address bar in web browser .

We can send limited amount of data through get request depends on web browser limit.

We should not use get method for login form because username and password details will be passed in web browser address bar. So they are visible to everyone.

$_GET variable is used to receive data from get request in PHP.

Also $_REQUEST variable is used to receive data from get request in PHP.

PHP Example :

File : register.php

<form action="thanks.php" method="get">   
    <table>   
        <tr><td>Username:</td><td> <input type="text" name="username"/></td></tr>
       <tr><td>Mobile:</td><td> <input type="text" name="mobile"/></td></tr>  
        <tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>   
        <tr><td></td><td><input type="submit" name="submit"  value="login"/>  </td></tr>  
    </table>  
</form>  

File : thanks.php

<?php  
    $username=$_GET["username"]; //receiving username field value by $_GET variable
    $password=$_GET["password"]; //receiving password field value by $_GET variable
    $mobile=$_GET["mobile"]; //receiving mobile field value by $_GET variable
     echo "Welcome, $username. your password is $password and your mobile is $mobile";  
 ?> 

Output:

Welcome, aryatechno. your password is learnphp and your mobile is 999999999

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

97378