Queue operations for php cookie operations

Source: Internet
Author: User

From the simplest cookie operations (add, delete, and modify) to our cookie queue operations, you can refer to this instance for more information.

1. Set Cookie

1. PHP COOKIE

Cookie is a mechanism for storing data in a remote browser and tracking and identifying users.
PHP sends cookies in the http header, so the setcookie () function must be output to the browser in other information.
This is similar to the limitation on the header () function.

1.1 set cookie:

You can use the setcookie () or setrawcookie () function to set the cookie. You can also directly send an http header to the client.
.
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
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
.

Here are several examples:

Simple:

SetCookie ("MyCookie", "Value of MyCookie ");

 

With expiration time:

The Code is as follows: Copy code

SetCookie ("WithExpire", "Expire in 1 hour", time () + 3600); // 3600 seconds = 1 hour

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; // 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 (empty ($ 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 have much to explain. This is not much used by others. I wrote it yesterday. I 'd like to leave it for later use ..
The called code 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 );

In this way, you can. Call .. You can change $ cookie_id to different values each time to verify this operation.
The following code can be used to verify the Code:

The Code is as follows: Copy code
Var_dump ($ _ COOKIE );

FAQs:

1) An error message is prompted when setcookie () is used, probably because there is an output or space before setcookie () is called. Or your text
Files are converted from other character sets. The document may be followed by a BOM signature (that is, add some hidden files in the file content ).
). The solution is to prevent this problem from occurring in your documents. You can also use the ob_start () function.
It can also handle a little bit.
2) $ _ COOKIE is affected by magic_quotes_gpc and may be automatically escaped.
3) it is necessary to test whether the user supports cookies.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.