Buy one of each fget the fourth free.

Up to 87% savings on Geeks.com!

SECRET CAMCORDER Prices too low to show! Expires 8/31
This sale includes 22 camcorders from Sony, JVC, Samsung, and Canon!

Take 10% Off All MCM Electronics Purcases!

HP Store search box: Computers, Electronics, Hardware

Search:

$_GET, $_POST, $_COOKIE, $_REQUEST - REQUEST VARIABLES, $_SESSION

   

By Sergey Skudaev


In this tutorial you will learn how to use request variables $_GET, $_POST, $_COOKIE, $_REQUEST and how to use session in PHP

You can use different ways to transfer data from one web page to another. Let us start with Form methods: GET and POST. First we will use GET method Create a simple HTML form.

$_GET[]

<html>
<head>
<title>Form Methods
</title>
</head>
<body>
<form method="get" action="formoutputpage.php">
<p><input type=text name=greeting size="15"></p>
<p><input type=text name=name size="15"></p>
<p><input type=submit name=submit value="Salutation"></p>
</form>
</body>
</html>

Save it as form_methods.php file in your Apache htdocs/post folder created by you.

Let us create a formoutputpage.php file for output data transferred from the form.

<?

echo $_GET['greeting'];

echo $_GET['name'];

echo "!";

?>

Save this file in the same directory as form_methods.php file.

This form looks looks that:

HTML form example

Let us enter a greeting and a name and click Salutation button.
You can see that data sent from a form with the GET method is displayed in browser's address bar:

http://localhost/post/formoutputpage.php?greeting=Hello&name=Emily&submit=Salutation

Output web page displays Hello Emily!

$_POST[]

Let us change POST method instead of GET method. Edit form_method.php form.

<html>
<head>
<title>Form Methods
</title>
</head>
<body>
<form method="post" action="formoutputpage.php">
<p><input type=text name=greeting size="15"></p>
<p><input type=text name=name size="15"></p>
<p><input type=submit name=submit value="Salutation"></p>
</form>
</body>
</html>

Edit formoutputpage.php file as follow:

<?

echo $_POST['greeting'];

echo " ".$_POST['name'];

echo "!";

?>

The browser address bar display formoutputpage.php, but no data transferred with POST Method is visible. Web page output will be the same:

Hello Emily!

$_COOKIE[]

Let us try to use $_COOKIE[ ] variable.

Edit form_methods.php and add the following piece of code. The cookie´s code must be inserted before HTML header. Other wise you will get error! The setcookie function will set $pref variable to $_COOKIE[] variable.

<?

$pref="Mrs";

setcookie("prefix",$pref);

?>

<html>
<head>
<title>Form Methods
</title>
</head>
<body>
<form method="post" action="formoutputpage.php">
<p><input type=text name=greeting size="15"></p>
<p><input type=text name=name size="15"></p>
<p><input type=submit name=submit value="Salutation"></p>
</form>
</body>
</html>

Edit formoutputpage.php file like that:

<?

echo $_POST['greeting'];

echo " ".$_COOKIE['prefix'];

echo " ".$_POST['name'];

?>

The output page displays

Hello Mrs. Emily.

$_SESSION[]

Let us try to use session variable. The session´s code must be inserted before HTML header. Other wise you will get error! Edit form_methods.php file:

<?

session_start();

$_SESSION['title']="Dr.";

setcookie("prefix","Mrs");

?>

<html>
<head>
<title>Form Methods
</title>
</head>
<body>
<form method="post" action="formoutputpage.php">
<p><input type=text name=greeting size="15"></p>
<p><input type=text name=name size="15"></p>
<p><input type=submit name=submit value="Salutation"></p>
</form>
</body>
</html>

Edit formoutputpage.php file.The session_start() function starts session. It must be used each time you assign value to $_SESSION variable or read value from $_SESSION variable.

<?

session_start();

echo $_POST['greeting'];

echo " ".$_SESSION['title'];

echo " ".$_COOKIE['prefix'];

echo " ".$_POST['name'];

?>

The output page will display:

Hello Dr. Mrs. Emily

You can transfer data from one page to another via link. Edit form_methods.php file. Date sent by link is transfered by get method. It can be read from $_GET[] variable

<?

session_start();

$_SESSION['title']="Dr.";

setcookie("prefix","Mrs");

?>

<html>
<head>
<title>Form Methods
</title>
</head>
<body>
<form method="post" action="formoutputpage.php">
<p><input type=text name=greeting size="15"></p>
<p><input type=text name=name size="15"></p>
<p><input type=submit name=submit value="Salutation"></p>
</form>
<?

$greeting="Good morning"

$person="Michael";

print('

Link output');

?>

</body>
</html>

When you hover mouse over the link, $GET variables are displayed on the browser task bar.

$_GET variable in task bar

Create link_output.php file.

<?

$greeting=$_GET['greeting'];

$person=$_GET['person'];

echo $greeting." ".$person."!";

?>

Click the link of form_methods.php page and you will the output:

Good morning Michael!

You can use $_REQUEST[] variable that contains contents of $_GET, $_POST and $_COOKIE variables. Edit formoutputpage.php.

<?

session_start();

echo $_REQUEST['greeting'];

echo " ".$_SESSION['title'];

echo " ".$_REQUEST['prefix'];

echo " ".$_REQUEST['name'];

?>

Fill the form and submit. Output will be the same: Hello Dr. Mrs. Emily!

Also you can insert statement import_request_variables("pgc",""); and use form input field names to access $_GET[], $_POST[] and $_COOKIE[]

Edit formoutputpage.php file like that:

<?

import_request_variables("pgc","");

session_start();

echo $greeting;

echo " ".$_SESSION['title'];

echo " ".$prefix;

echo " ".$name;

?>

Submit form again and you will get the same output: Hello Dr. Mrs. Emily!

Click link on the form and you will get output: Good morning Michael!

Please rate the tutorial

1 2 3 4 5



Custom Search