|
|
|
The below code is a simple example of Cookies.
Once the cookie is set, you may read the cookie by accessing cookie2.php for up to 24 hours. After that, the cookie will expire.
To see the example at work, please review:
http://www.xn--ovg.com/cookie <-- set cookie
http://www.xn--ovg.com/cookie/getcookie.php <-- read cookie
tedd
--- copy below and save as cookie1.php -----
|
<?php
$user = $_POST['user'];
$color = $_POST['color'];
$self = $_SERVER['PHP_SELF'];
if( ( $user != null ) and ( $color != null ) )
{
setcookie( "firstname", $user , time() + 86400 ); // 24 hours
setcookie( "fontcolor", $color, time() + 86400 );
header( "Location:cookie2.php" );
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
<title>Stuff by tedd</title>
</head>
<body>
<h1>tedd's cookie stuff</h1>
<hr>
<form action ="<?php echo( $self ); ?>" method = "post">
Please enter your first name:
<input type = "text" name = "user"><br><br>
Please choose your favorite font color:<br>
<input type = "radio" name = "color" value = "Red">Red
<input type = "radio" name = "color" value = "Green">Green
<input type = "radio" name = "color" value = "Blue">Blue
<br><br>
<input type = "submit" value = "submit">
</form>
<br/>
<hr>
</body>
</html>
</html> | |
--- copy below and save as cookie2.php -----
| <?php
if (isset($_COOKIE['firstname']))
{
$user = $_COOKIE['firstname'];
$color= $_COOKIE['fontcolor'];
}
else
{
$user = $_POST['user'];
$color = $_POST['color'];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
<title>Stuff by tedd</title>
</head>
<body>
<h1>tedd's show cookie stuff</h1>
<hr>
<h2>Hello: <?php echo( $user ); ?> </h2>
<h2>Your color: <?php echo( $color ); ?> </h2>
<hr>
<br/>
<?php
// Another way to debug/test is to view all cookies
echo ("<br/>");
echo ("<pre>");
echo ("Cookie info:\n");
print_r($_COOKIE);
echo("</pre>");
?>
<p>
<a><input type="button" value="back" onclick="history.go(-1)"></a>
</p>
</body>
</html> | | |
|
| A very simple PHP single password cookie based login without usernames. Categories : PHP, Cookies, Security, Beginner Guides | | | Store, retrieve and delete cookies using JavaScript. Categories : Java Script, Cookies, Beginner Guides, Cookies | | | Simple PHP cookie counter Categories : PHP, Cookies, Beginner Guides | | | A flat file counter Categories : PHP, Cookies, Filesystem, Beginner Guides | | | PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL | | | With this class you can use cookies with chips Categories : PHP, Cookies | | | Current Page's URL using PHP Categories : PHP, Beginner Guides, Global Variables | | | Get the self URL of current page Categories : PHP, URLs, Beginner Guides | | | Newbie Notes #1 - Making a form return to itself Categories : PHP, Beginner Guides, HTML and PHP | | | Function that does language negotiation based on the Accept-Language header, a cookie or host name Categories : HTTP, PHP, Cookies | | | Cross Browser Session Starter Categories : PHP, Sessions, Cookies | | | Automatic global variable definer Categories : PHP, Variables, Beginner Guides | | | Example voting script. Lets people enter suggestions and vote for existing ones. Categories : MySQL, PHP, Cookies, Complete Programs, Databases | | | Multiple Select box, Select multiple Items from Menu.List box Categories : PHP, HTML and PHP, Beginner Guides | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | |
|
|