Php uses the cookie queue to operate queue instances. Php queue operation instances for cookie operations. cookie queue this article describes the queue operation classes for php cookie operations. Share it with you for your reference. The specific analysis is as follows: This php queue operation instance for cookie operations, cookie queue
This example describes the queue operation class of php for cookie operations. Share it with you for your reference. The specific analysis is as follows:
This includes operations from simple cookie operations (add, delete, modify) to our cookie queue operations. if you are interested in this operation, refer to it.
I. PHP cookies
Cookie is a mechanism for storing data in a remote browser and tracking and identifying users.
PHP sends cookies in the http header. Therefore, the setcookie () function must be called before other information is output to the browser. This is similar to the header () function.
Set cookie:
You can use the setcookie () or setrawcookie () function to set the cookie, or you can directly send an http header to the client to set the cookie.
Use the setcookie () function to set the cookie:
The code is as follows:
Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure [, bool httponly])
Parameters:
Name: cookie variable name
Value: The value of the cookie variable.
Expire: the end time of the validity period
Path: valid directory
Domain: valid domain name, unique in top-level domain
Secure: if the value is 1, the cookie can only be valid for https connections. if the default value is 0, both http and https can be used.
Here are a few examples:
The code is as follows:
SetCookie ("MyCookie", "Value of MyCookie ");
The code with the expiration time is as follows:
The code is as follows:
SetCookie ("WithExpire", "Expire in 1 hour", time () + 3600); // 3600 seconds = 1 hour
The code is as follows:
The code is as follows:
SetCookie ("FullCookie", "Full cookie value", time () + 3600, "/forum", ".phpuser.com", 1 );
We need to use the queue. the code is as follows:
The code is as follows:
Class QueueSvc
{/*{{{*/
Private $ length; // queue length
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: push ($ server_name );
}
Private function push ($ server_name)
{
// Delete duplicate records
If (self: isServerExist ($ server_name )){
Self: removeRepeat ($ server_name );
} Else {
If (self: isFull ()){
// If the queue is full, delete the last record of the queue.
Array_pop ($ this-> server_arr );
}
}
// If the queue is empty, set it to an empty array first.
If (emptyempty ($ this-> server_arr ))
$ This-> server_arr = array ();
// Add data to the 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 ,'/');
}
}
}/*}}}*/
}/*}}}*/
I don't need to explain much. this is not used much by others. I leave it for yesterday because I need to write it. maybe it will be used later. the called code is very simple. the code is as follows:
The code is as follows:
Require_once ("queue_svc.php ");
Require_once ("cookie_svc.php ");
$ Cookie_id = '4 ';
CookieSvc: set ($ cookie_id );
In this way, you can change $ cookie_id to a different value each time to verify this operation. the code for this test can be used as follows:
The code is as follows:
Var_dump ($ _ COOKIE );
II. solutions to common problems:
1. an error message is prompted when setcookie () is used, probably because there is an output or space before setcookie () is called. Or your document may be converted from other character sets. the document may be followed by a BOM signature (that is, add some hidden BOM characters to the file content ), the solution is to prevent this problem in your document. you can also use the ob_start () function to handle this problem.
2. $ _ COOKIE is affected by magic_quotes_gpc and may be automatically escaped.
3. during use, it is necessary to test whether the user supports cookies.
I hope this article will help you with PHP programming.
The example in this article describes the queue operation class of php for cookie operations. Share it with you for your reference. The specific analysis is as follows...