PHP Queue action class instance for cookies _php tips

Source: Internet
Author: User
Tags setcookie

The example in this article describes the queue action class for the cookie operation for PHP. Share to everyone for your reference. The specific analysis is as follows:

This includes operations from simple cookie operations (add, delete, modify) to our cookie queue operations class, which interested friends can refer to.

One, PHP cookies

A cookie is a mechanism for storing data on a remote browser side to track and identify the user.

PHP sends cookies in the header information of the HTTP protocol, so the Setcookie () function must be called before other information is exported to the browser, similar to the limit on the header () function.

To set Cookies:

You can set cookies by using the Setcookie () or Setrawcookie () function, or by sending HTTP headers directly to the client.

Here, use the Setcookie () function to set cookies:

Copy Code code as follows:
BOOL Setcookie (string name [, string value [, int expire [, String path [, string domain [, bool secure [, BOOL HttpOnly]]]] ] )

The parameters:

Name:cookie variable Name

The value of the Value:cookie variable

Expire: Time at the end of validity

Path: Valid directory

Domain: Valid domain name, top-level domain unique

Secure: If the value is 1, the cookie can only be valid on the HTTPS connection, and HTTP and HTTPS are available if the default is 0.

Take a look at a few examples, simple:

Copy Code code as follows:
Setcookie ("MyCookie", "Value of MyCookie");

With the expiration time of the. Code as follows:

Copy Code code as follows:
Setcookie ("Withexpire", "Expire in 1 Hour", Time () +3600);//3600 seconds = 1 hours

Everything, the code is as follows:

Copy Code code as follows:
Setcookie ("Fullcookie", "full cookie value", Time () +3600, "/forum", ". phpuser.com", 1);

We need to use the queue, the code is as follows:

Copy Code code as follows:
Class Queuesvc
{/*{{{*/
Private $length; Length of 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 of the queue.
Array_pop ($this->server_arr);
}
}
If the queue is empty, set first to an empty array
if (Emptyempty ($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, stay for a while, perhaps later still use, call code is very simple, the code is as follows:
Copy Code code as follows:
Require_once ("queue_svc.php");

Require_once ("cookie_svc.php");

$cookie _id = ' 4 ';

Cookiesvc::set ($cookie _id);

This can be done, you can change the $cookie_id each time to do different values, to verify this operation, the code can be used to test the following code:
Copy Code code as follows:
Var_dump ($_cookie);

Second, common problem solving:

1. When using Setcookie (), there is an error prompt, possibly because the call Setcookie () has an output or a space before it. It may also be that your document is converted from other character sets, and the document may be followed by a BOM signature (that is, adding some hidden BOM characters to the contents of the file), and the solution is to keep your document out of this situation and to handle a bit by using the Ob_start () function.

2. $_cookie is affected by MAGIC_QUOTES_GPC and may be automatically escaped.

3. When used, it is necessary to test whether the user supports cookies.

I hope this article will help you with your PHP program design.

Related Article

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.