PHP Exercise 5
Cookies
<?php
setcookie("TestCookie","$value",time()+3600);
...
setcookie(string name, string value, int expire, string path, string domain, int secure)
<?php
// The isset() function returns true if a
variable has a
// value
if (isset($_GET[test_info]))
{
// No output has occurred so it's OK to have
setcookie()
// function here. The time()+3600 means that this
cookie
// will expire in one hour (3600 secs = 1 hour)
setcookie ("TestCookie","$_GET[test_info]",time()+3600);
echo "Now test your cookie<br>";
echo "<a
href=\"cookie_test2.php\">cookie_test2.php</a>";
}
else
{
// The action=\"$_SERVER[PHP_SELF]\" will call
cookie_test.php again, but
// when it calls it again, $_Get[test_info] will have
a value
// and this part will not be executed.
echo "Enter your username:<br>
<form action=\"$_SERVER[PHP_SELF]\"
method=\"Get\">
<input type=\"text\" name=\"test_info\">
<input type=\"submit\"
value=\"Submit\"></form>";
}
?>
·
test_info is just the
name of the variable that I decided to make up. You can replace test_info with anything you like!
·
$_SERVER[PHP_SELF]
will contain the value of cookie_test.php. So this
page will be called again FROM THE TOP OF THE CODE. The second time your
browser loads this page, $_GET[test_info] will have a value and the code under the IF statement will be executed.
Also note this method DOES require you to test to see if the form has been seen
by the user using an IF statement.
<?php
// Test to see if the cookie has been set
correctly
if (isset($HTTP_COOKIE_VARS["TestCookie"]))
{
echo "The cookie is set<br>Here is the value:
";
// To get the value of our cookie we named
TestCookie, we use
// the php global variable array $HTTP_COOKIE_VARS[]
// Notice the lack of "" around this
echo statement!
echo $HTTP_COOKIE_VARS["TestCookie"];
echo "<br>Do you want to log
out?<br>";
echo "<a
href=\"cookie_test_logout.php\">Yes</a> | <a
href=\"$_SERVER[PHP_SELF]\">No</a>";
}
else
{
echo "the cookie is not set";
echo "<br>Return to the <a
href=\"cookie_test.php\">Login page</a>";
}
?>
<?php
// This cookie will expire 1 hour ago,
effectively
// removing the cookie information
setcookie("TestCookie","",time()-3600);
echo "You are now logged out";
echo "<br>Return to the <a
href=\"cookie_test.php\">Login page</a>";
echo "<br>Return to the <a href=\"cookie_test2.php\">cookie
test page</a>";
?>
11. In an e-commerce website it would be VERY wise to
match a users login name and password with information in a database before
setting a cookie.
·
Create a page that
first tests if the user has a cookie with the required information (username
only). If not, provide a link to the
second file (which is the login page). If they do have a cookie with a value,
then display the message "Welcome back username!"
(Where username
is the value stored in the cookie).
· The second .php file is the actual login page. The following is pseudocode. You must turn this into the actual .php file.
<?php
IF(isset($_GET[username_from_form]))
{
Connect
to mysql database
$query
= Select * from customers where username=$_GET[username_from_form]
.
. . All the other stuff that goes with a DB query
$db_result
= mysql_fetch_row[$result]
// $db_result[0] is the username field from the table
// $db_result[1] is the password field from the table
// This is true only if your first field in your database
is the username
// and the second is the password field.
If ($_GET[password_from_form]
== $db_result[1])
{
setcookie
with value=$_GET[username]
}
else
{
Display
“Login was incorrect. Please try again”
Display
HTML code for form
}
}
else
{
Display
HTML code for form. Make sure action=$_SERVER[PHP_SELF] & method=POST
}
?>