The article from the simplest cookie operation (add, delete, modify) to our cookie queue operation class, there is a need to understand the students can refer to this example.
1. Set Cookies
1. PHP Cookies
A cookie is a mechanism for storing data on a remote browser to track and identify users.
PHP sends a cookie in the header of the HTTP protocol, so the Setcookie () function must be exported to the browser in other information
The previous call, which is similar to the limit on the header () function.
1.1 Setting Cookies:
You can use the Setcookie () or Setrawcookie () function to set the cookie. You can also send HTTP headers directly to the client to
Set up.
1.1.1 Use the Setcookie () function to set the cookie:
BOOL Setcookie (string name [, string value [, int expire [, String path [, string domain [, bool secure [, BOOL
HttpOnly]])
Name:cookie variable Name
The value of the Value:cookie variable
Expire: Time of expiry
Path: Valid directory
Domain: Valid domain name, top-level domain unique
Secure: If the value is 1, the cookie is valid only on HTTPS connections, and if it is the default value of 0, both HTTP and HTTPS can be
To.
Take a look at some examples:
Simple:
Setcookie ("MyCookie", "Value of MyCookie");
With time of failure:
| The code is as follows |
Copy Code |
|
Setcookie ("Withexpire", "Expire in 1 Hour", Time () +3600);//3600 seconds = 1 hours |
There are everything:
| The code is as follows |
Copy Code |
|
Setcookie ("Fullcookie", "full cookie value", Time () +3600, "/forum", ". phpuser.com", 1); |
We need to use the queue.
| The code is as follows |
Copy Code |
|
Class Queuesvc
{/*{{{*/
Private $length; The length of the queue
Private $server _arr;
Public function __construct ($length, $server _arr)
{
$this->length = $length;
$this->server_arr = $server _arr;
}
Public Function Getserverarr ()
{
return $this->server_arr;
}
Public function set ($server _name)
{
Self::p ush ($server _name);
}
Private function push ($server _name)
{
Have duplicate records, delete duplicates.
if (Self::isserverexist ($server _name)) {
Self::removerepeat ($server _name);
}else{
if (Self::isfull ()) {
If it's full, delete the last record in the queue.
Array_pop ($this->server_arr);
}
}
If the queue is empty, first set to an empty array
if (Empty ($this->server_arr))
$this->server_arr = Array ();
Adding data to a queue header
Array_unshift ($this->server_arr, $server _name);
}
Private Function Isfull ()
{
if (Is_array ($this->server_arr) && (count ($this->server_arr) >= $this->length))
return true;
return false;
}
Private Function Isserverexist ($server _name)
{
if (Is_array ($this->server_arr) && In_array ($server _name, $this->server_arr))
return true;
return false;
}
Private Function Removerepeat ($server _name)
{
if (Is_array ($this->server_arr) && In_array ($server _name, $this->server_arr))
{
foreach ($this->server_arr as $key = $value)
{
if ($server _name = = $value)
{
$this->array_remove ($this->server_arr, $key);
}
}
}
}
Private Function Array_remove (& $arr, $offset) {
Array_splice ($arr, $offset, 1);
}
}/*}}}*/require_once (' queue_svc.php ');
Class Cookiesvc
{/*{{{*/
Const COOKIE_KEY = "Game_server";
Const SEPARATE = "|";
Const COOKIE_LENGTH = "2";
Public Function Getcookiearr ()
{/*{{{*/
$server _str = $_cookie[self::cookie_key];
$server _str = $_cookie[' game_server ');
if ($server _str = = ") {
$result = Array ();
}else{
$result = Explode (self::separate, $server _str);
}
return $result;
}/*}}}*/
Public function set ($cookie _id)
{/*{{{*/
$server _arr = Self::getcookiearr ();
if ($cookie _id! = False)
{
$que = new Queuesvc (self::cookie_length, $server _arr);
$que->set ($cookie _id);
$server _new = $que->getserverarr ();
if (Is_array ($server _new))
{
$cookie _str = Implode (self::separate, $server _new);
Setcookie (Self::cookie_key, $cookie _str,time () +3600, '/');
}
}
}/*}}}*/
}/*}}}*/
|
Not much explanation, this other people use not much, yesterday because need to write, leave a bit, perhaps later also use to get.
The code that is called is simple:
| The code is as follows |
Copy Code |
|
Require_once ("queue_svc.php");
Require_once ("cookie_svc.php");
$cookie _id = ' 4 ';
Cookiesvc::set ($cookie _id); |
That's all you can do. Call.. You can check this operation by changing the $cookie_id to a different value each time.
The code for the test can be in the following code:
| The code is as follows |
Copy Code |
| Var_dump ($_cookie); |
Frequently asked questions resolved:
1) There is an error when using Setcookie (), possibly because there is an output or a space in front of the call Setcookie (). Or maybe your text.
Files are converted from other character sets, which may be followed by a BOM signature (that is, adding some hidden
BOM characters). The solution is to keep your documents from happening. And by using the Ob_start () function
can also handle a little.
2) $_cookie is affected by MAGIC_QUOTES_GPC and may be automatically escaped.
3) When using, it is necessary to test whether the user supports cookies
http://www.bkjia.com/PHPjc/444690.html www.bkjia.com true http://www.bkjia.com/PHPjc/444690.html techarticle The article from the simplest cookie operation (add, delete, modify) to our cookie queue operation class, there is a need to understand the students can refer to this example. 1, set the cookie 1. PHP's ...