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.
Try out following example by putting the source code in test.php script.
<?php
if( $_GET["name"] || $_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>
|
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 you can make
sure that your information is secure.
- The PHP provides $_POST associative array to access all the sent information using POST method.
Try out following example by putting the source code in test.php script.
<?php
if( $_POST["name"] || $_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 PHP $_REQUEST variable contains the contents of both $_GET,
$_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will
explain about cookies.
The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.
Try out following example by putting the source code in test.php script.
<?php
if( $_REQUEST["name"] || $_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>
|
Here $_PHP_SELF variable contains the name of self script in which it is being called.
You can include the content of a PHP file into another PHP file
before the server executes it. There are two PHP functions which can be
used to included one PHP file into another PHP file.
- The include() Function
- The require() Function
This is a strong point of PHP which helps in creating functions,
headers, footers, or elements that can be reused on multiple pages.
This will help developers to make it easy to change the layout of
complete website with minimal effort. If there is any change required
then instead of changing thousand of files just change included file.
The include() Function
The include() function takes all the text in a specified file and
copies it into the file that uses the include function. If there is any
problem in loading a file then the
include() function generates a warning but the script will continue execution.
Assume you want to create a common menu for your website. Then create a file menu.php with the following content.
<a href="http://www.tutorialspoint.com/index.htm">Home</a> -
<a href="http://www.tutorialspoint.com/ebxml">ebXML</a> -
<a href="http://www.tutorialspoint.com/ajax">AJAX</a> -
<a href="http://www.tutorialspoint.com/perl">PERL</a> <br />
|
Now create as many pages as you like and include this file to create
header. For example now your test.php file can have following content.
<html>
<body>
<?php include("menu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
|
This will produce following result
Home -
ebXML -
AJAX -
PERL
This is an example to show how to include PHP file. You can include menu.php file in as many as files you like! |
The require() Function
The require() function takes all the text in a specified file and
copies it into the file that uses the include function. If there is any
problem in loading a file then the
require() function generates a fatal error and halt the execution of the script.
So there is no difference in require() and include() except they
handle error conditions. It is recommended to use the require()
function instead of include(), because scripts should not continue
executing if files are missing or misnamed.
You can try using above example with require() function and it will
generate same result. But if you will try following two examples where
file does not exist then you will get different results.
<html>
<body>
<?php include("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>
|
This will produce following result
This is an example to show how to include wrong PHP file!
|
Now lets try same example with require() function.
<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>
|
This time file execution halts and nothing is displayed.
NOTE: You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
No comments:
Post a Comment