Friday 28 February 2014

PHP crash course -4 ( GET POST & REQUEST function )

We Know PHP is server side language i.e. it execute on server . NOW  user have to insert a data and you have to send this data to server where your PHP code complete operation on that data Then send it back to User . The Problem is How to send this data for this purpose PHP have two Inbuilt feature . They are two Predefined function GET & POST .

Now See How they Work .

First of all make a form which take input Like Name & age. Bye user .
A sample Form for GET method :



 <html>  
 <body>  
 <center>  
 <form action="welcome.php" method="get">  
 NAME :<input type="text" name="fname"/><br />  
 AGE :<input type="text" name="fage"/><br />  
 <input type="submit" />  
 </form>  
 </center>  
 </body>  
 </html>  

This is a sample form which take name and age as input . send this input to welcome.php by method of GET . And fname & fage are variable store data Name and age given by user .

Related Post Easiest Way to Create Ad hoc Network (without any Software)

Now Make your welcome.php page where these data is processed to find desire result .

Sample welcome.php page for get method :


 <?php  
 // print data obtain by GET method;  
 echo "your name is ".$_GET["fname"]." and your age is ".$_GET["fage"] ;  
 ?>  

Here we receive data send by form & in this example we simply print this data . In this way GET function works.

Now you Can see there is a problem In GET function that it shows The data you send in URL . That is why it is not much secure so not use for secure purposes .
It has another drowback that it can't send data greter than 100 char at a time (this data may change depend on version of php ) in other word length of data to send is also limited .

Now move to  POST function . Now send and recieve data by method of post it is similar to GET in manner of code Just replace  GET By POST . Anyway I put code below .

Related Post : How to host your website for FREE (google drive )

Sample form for post method :


 <html>  
 <body>  
 <center>  
 <form action="welcome2.php" method="POST">  
 NAME :<input type="text" name="fname"/><br />  
 AGE :<input type="text" name="fage"/><br />  
 <input type="submit" />  
 </form>  
 </center>  
 </body>  
 </html>  

Every thing is smiler as GET method  form . JUSt method of sending data change to POST .

Sample welcome.php page for POST method :


 <?php  
 // print data obtain by GET method;  
 echo "your name is ".$_POST["fname"]." and your age is ".$_POST["fage"] ;  
 ?> 

It is also same as welcome .php of GEtT function . We just Replace GET by POST .

Here the data are send and receive by post method .

You Can see In this Case no data is visible in URL so it is secure than GET and also it does not have size of data limit to send .
This method can be use to send sensitive data .

NOTE : There is an 8 Mb max size for the POST method ,by default ( i.e. it can be change by setting the post_max_size in php.ini   file )

Related Post : How to free your Browser from unwanted search extensions

NOw  Its time For REQUEST function :

There are Many scenario  when you have to receive data by both method . In other word you don't know by which method data is send  Or may be you have to receive data bye many source and all of then uses different method to send data . Such situation Importance of request function dominated .

The main Property of REQUEST function is it can receive data send by any method GET or POST.

you can use any form from above to send data
and to receive use this

Sample welcome.php page for REQUEST  method :


 <?php  
 // print data obtain by GET method;  
 echo "your name is ".$_REQUEST["fname"]." and your age is ".$_REQUEST["fage"] ;  
 ?> 

Now check This function By yourselves .

Related Post PHP crash course -1

                 PHP crash course -2

                 PHP crash course -3


Thanks have a NICE DAY :)

1 comment:

  1. Heya just wanted to give you a brief heads up and let you know a few of the images aren't loading properly.

    I'm not sure why but I think its a linking issue. I've tried it in two different browsers
    and both show the same outcome.

    Stop by my web site: Beats By Dr Dre

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets