php queue operation class instance for cookie operation, cookie queue
The example in this article describes PHP’s queue operation class for cookie operations. Share it with 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. Friends who are interested in this can refer to it.
1. PHP COOKIE
A cookie is a mechanism that stores data on a remote browser to track and identify users.
PHP sends cookies in the header information of the http protocol, so the setcookie() function must be called before other information is output to the browser, which is similar to the restriction on the header() function.
Set cookie:
You can use the setcookie() or setrawcookie() function to set cookies, or you can set them by sending http headers directly to the client.
Use the setcookie() function here to set cookies:
Copy code 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: value of cookie variable
expire: the time when the validity period ends
path: valid directory
domain: valid domain name, unique top-level domain
secure: If the value is 1, the cookie is only valid on https connections. If it is the default value 0, both http and https are valid.
Let’s look at a few examples, simple:
Copy code The code is as follows:
SetCookie("MyCookie", "Value of MyCookie") ;
With expiration time. The code is as follows:
Copy code The code is as follows:
SetCookie("WithExpire", "Expire in 1 hour", time()+3600);//3600 seconds =1 hour
Everything is available, the code is as follows:
Copy code The code is as follows:
SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", " .phpuser.com", 1);
We need to use a queue, the code is as follows:
Copy code The code is as follows:
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::push($server_name);
}
Private function push($server_name)
{
//If there are duplicate records, delete the duplicates
If(self::isServerExist($server_name)){
self::removeRepeat($server_name);
}else{
If(self::isFull()){
//If it is full, delete the last record in 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 head
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,'/');
}
}
}/*}}}*/
}/*}}}*/
不多解释了,这个别人用的不多,昨天因为需要写的,留一下吧,也许以后还用得到,调用的代码很简单,代码如下:
复制代码 代码如下:
require_once("queue_svc.php");
require_once("cookie_svc.php");
$cookie_id = '4';
CookieSvc::set($cookie_id);
这样就可以了,大家可以每次把$cookie_id换做不同的值,来检验此操作,检验的代码可以用如下代码:
复制代码 代码如下:
var_dump($_COOKIE);
二、常见问题解决:
1. 用 setcookie()时有错误提示,可能是因为调用setcookie()前面有输出或空格。也可能你的文档是从其他字符集转换过来,文档后面可能带有 BOM 签名(就是在文件内容添加一些隐藏的BOM 字符),解决的办法就是使你的文档不出现这种情况,还有通过使用ob_start()函数也能处理一点.
2. $_COOKIE 受magic_quotes_gpc 影响,可能自动转义.
3. 使用的时候,有必要测试用户是否支持cookie.
希望本文所述对大家的PHP程序设计有所帮助。
http://www.bkjia.com/PHPjc/925133.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/925133.htmlTechArticlephp针对cookie操作的队列操作类实例,cookie队列 本文实例讲述了php针对cookie操作的队列操作类。分享给大家供大家参考。具体分析如下: 这...