0% found this document useful (0 votes)
143 views

GET and POST Method in PHP

The document discusses the GET and POST methods for sending data from a browser to a web server in PHP. The GET method appends encoded data to the URL, is limited to 1024 characters, and is not secure for passwords. The POST method encodes data into HTTP headers, has no size limit, and can send binary data more securely by using HTTPS. Both methods can access submitted data through the $_GET, $_POST, and $_REQUEST superglobal arrays in PHP.

Uploaded by

waqar2901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

GET and POST Method in PHP

The document discusses the GET and POST methods for sending data from a browser to a web server in PHP. The GET method appends encoded data to the URL, is limited to 1024 characters, and is not secure for passwords. The POST method encodes data into HTTP headers, has no size limit, and can send binary data more securely by using HTTPS. Both methods can access submitted data through the $_GET, $_POST, and $_REQUEST superglobal arrays in PHP.

Uploaded by

waqar2901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

GET and POST method in PHP



There are two ways the browser client can send information to the web server.

The GET Method
The POST Method

Before the browser sends the information, it encodes it using a scheme called URL encoding. In this
scheme, name/value pairs are joined with equal signs and different pairs are separated by the
ampersand.

name1=value1&name2=value2&name3=value3

Spaces are removed and replaced with the + character and any other nonalphanumeric characters
are replaced with a hexadecimal values. After the information is encoded it is sent to the server.

The GET Method
The GET method sends the encoded user information appended to the page request. The page and
the encoded information are separated by the ? character.

http://www.test.com/index.htm?name1=value1&name2=value2

The GET method produces a long string that appears in your server logs, in the browser's
Location: box.
The GET method is restricted to send upto 1024 characters only.
Never use GET method if you have password or other sensitive information to be sent to the
server.
GET can't be used to send binary data, like images or word documents, to the server.
The data sent by GET method can be accessed using QUERY_STRING environment variable.
The PHP provides $_GET associative array to access all the sent information using GET
method.

Example Source Code: test.php

<?php
if( isset($_GET["name"]) && isset($_GET["age"] ))
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>



GET and POST method in PHP

2

The POST Method
The POST method transfers information via HTTP headers. The information is encoded as
described in case of GET method and put into a header called QUERY_STRING.

The POST method does not have any restriction on data size to be sent.
The POST method can be used to send ASCII as well as binary data.
The data sent by POST method goes through HTTP header so security depends on HTTP
protocol. By using Secure HTTP (https) you can make sure that your information is secure.
The PHP provides $_POST associative array to access all the sent information using GET
method.

Example Source Code: test.php

<?php
if( isset($_POST["name"]) && isset($_POST["age"] ))
{
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

The $_REQUEST Variable
The $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The PHP
$_REQUEST variable can be used to get the result from form data sent with both the GET and POST
methods.

Example Source Code: test.php

<?php
if(isset($_REQUEST["name"]) && isset($_REQUEST["age"] ))
{
echo "Welcome ". $_REQUEST['name']. "<br />";
echo "You are ". $_REQUEST['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

GET and POST method in PHP

3
Here $_PHP_SELF variable contains the name of self script (test.php) in which it is being called.
isset returns TRUE if variable exists and has value other than NULL, FALSE otherwise.

Output of the above examples:

You might also like