Thursday, December 25, 2014

GET and POST in PHP

GET and POST  are the basic way how a browser sends information to the web server while working with PHP.

The Get Method:
It is used to send encoded user information appended to the page request. On using GET method  the page and the encoded information are separated by the "?" character. This method has restriction to send only up to 1024 characters. This method isn't used if you are processing sensitive information for sending it to the server. $_GET associative array is used by PHP to  access all the sent information.


Example:

PHP Script (get.php)

 <?php  
 if(isset($_GET['name']) || isset($_GET['website']))  
  {  
    echo "Welcome ". $_GET['name']. ",<br />";  
    echo "I am from ". $_GET['website'].".";  
  }else  
  {  
   echo "Please write name and website.";  
  }  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
 <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <form action ="<?php $_PHP_SELF; ?>" method = "GET">  
 Name:<input type = "text" name ="name" />  
 Website:<input type = "text" name = "website"/>  
 <input type ="submit" value = "Submit!" />  
 </form>  
 </body>  
 </html> 

The POST Method:

This method transfers information via HTTP headers. POST information is encoded as like same as in GET method and put into a header called QUERY_STRING. This method do not have any restriction in data size. Any kind of data can be sent using this method. Information sent using this method is more secure but it also depends on used HTTP protocol.

Example:

PHP Script (post.php)
<?php  
 if(isset($_POST['name']) and isset($_POST['website']))  
  {  
   if(!empty($_POST['name'])){  
    echo "Welcome ". $_POST['name']. ",<br />";  
    echo "I am from ". $_POST['website'].".";  
   }else{  
    echo "Name can't be blank!";  
   }  
  }else  
  {  
   echo "Please write name and website Name.";  
  }  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
 <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <form action ="<?php $_PHP_SELF; ?>" method = "POST">  
 Name:<input type = "text" name ="name" />  
 Website Name:<input type = "text" name = "website"/>  
 <input type ="submit" value = "Submit!" />  
 </form>  
 </body>  
 </html>  

No comments:

Post a Comment