PHP/Cookie Session/setcookie
Содержание
- 1 Assigning cookie names according to array index value
- 2 Cookie created
- 3 Deleting a Cookie Using setcookie()
- 4 Deleting Cookies
- 5 PHP 5 setcookie() Parameters
- 6 Reading Cookies
- 7 Setting a cookie with a domain restriction
- 8 Setting a cookie with a path restriction
- 9 Setting an expiring cookie
- 10 Setting cookie expiration
- 11 Setting the cookie domain
- 12 Setting the cookie path
- 13 Setting the cookie path to a specific directory
- 14 Starting a page with setcookie()
- 15 The cookie expires one day from now
Assigning cookie names according to array index value
<?
setcookie("php[uid]", "1111111111111111", time()+3600);
setcookie("php[color]", "black", time()+3600);
setcookie("php[preference]", "english", time()+3600);
if (isset ($php)) {
while (list ($name, $value) = each ($php)) {
echo "$name = $value<br>\n";
}
}
?>
Cookie created
<?php
echo( "Cookie created? : " . setcookie("cookie_name", "cookie_data" ) );
?>
<html>
<head>
<title>Cookie</title>
</head>
<body>
</body>
</html>
Deleting a Cookie Using setcookie()
<?php
setcookie("mycookie", NULL, NULL, "/", ".demo.org");
?>
Deleting Cookies
<?php
setcookie ( "cookie_user", "test", time () + 60 * 60 * 24 * 30 );
setcookie ( "cookie_pass", md5 ( "test" ), time () + 60 * 60 * 24 * 30 );
function logout() {
setcookie ( "cookie_user", "", time () + 60 * 60 * 24 * 30 );
setcookie ( "cookie_pass", "", time () + 60 * 60 * 24 * 30 );
}
logout ();
echo $_COOKIE ["cookie_user"] . "<br />";
echo "You have successfully logged out.";
?>
PHP 5 setcookie() Parameters
Parameter Description
name The name to set the cookie variable to and hence the name to access it with
value The value of the current cookie
expire When a cookie will expire (in the form of a Unix timestamp)
path The directory where the cookie will be available for use
domain The domain at which the cookie will be available
secure Whether a cookie can be read on a non-SSL enable script
<?php
$GLOBALS["username"] = "test";
$GLOBALS["password"] = "test";
function validatelogin ($username, $password){
if (strcmp ($username, $GLOBALS["username"]) == 0 && strcmp ($password, $GLOBALS["password"]) == 0){
setcookie ("cookie_user", $username, time()+60*60*24*30);
setcookie ("cookie_pass", md5 ($password), time()+60*60*24*30);
return true;
} else {
return false;
}
}
if (validatelogin ("test","test")){
echo "Successfully logged in.";
} else {
echo "Sorry, invalid login.";
}
?>
Reading Cookies
<?php
$GLOBALS["username"] = "test";
$GLOBALS["password"] = "test";
setcookie ("cookie_user", "test", time()+60*60*24*30);
setcookie ("cookie_pass", md5 ("test"), time()+60*60*24*30);
function validatelogin (){
if (strcmp ($_COOKIE["cookie_user"], $GLOBALS["username"]) == 0 && strcmp ($_COOKIE["cookie_pass"], md5 ($GLOBALS["password"])) == 0){
return true;
} else {
return false;
}
}
if (validatelogin ()){
echo "Successfully logged in.";
} else {
echo "Sorry, invalid login.";
}
?>
Setting a cookie with a domain restriction
<?php
setcookie("flavor","chocolate","","",".example.ru");
setcookie("flavor","chocolate","","","j.example.ru");
?>
Setting a cookie with a path restriction
<?php
setcookie("flavor","chocolate","","/products/");
?>
Setting an expiring cookie
<?php
setcookie("flavor","chocolate",1259841600);
?>
Setting cookie expiration
// The cookie expires one hour from now
setcookie("short-userid","AAA",time() + 60*60);
Setting the cookie domain
setcookie("short-userid","AAA",0,"/",".example.ru");
Setting the cookie path
setcookie("short-userid","AAA",0,"/");
Setting the cookie path to a specific directory
setcookie("short-userid","AAA",0,"/~alice/");
Starting a page with setcookie()
<?php
setcookie("userid","AAA");
?>
<html><head><title>Page with cookies</title><head>
<body>
This page sets a cookie properly, because the PHP block
with setcookie() in it comes before all of the HTML.
</body></html>
The cookie expires one day from now
setcookie("longer-userid","AAA",time() + 60*60*24);