Computer Programming web Web programming Tips



PART II:$ _COOKIE & $ _SESSION

By Sergey Skudaev


$_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('<p><a href="link_output.php?greeting='.$greeting.'&person='.$person.'">Link output</a>');

?>

</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!

User Authorization with Session Cookies

My eBooks on Amazon.com

US    UK    BR    CA
US   UK   BR   CA
US    UK    BR    CA