Home > Backend Development > PHP Tutorial > PHP cookie operation queue operation class_PHP tutorial

PHP cookie operation queue operation class_PHP tutorial

WBOY
Release: 2016-07-20 11:10:48
Original
759 people have browsed it

The article ranges from the simplest cookie operations (add, delete, modify) to our cookie queue operations. Students who need to know more can refer to this example.

1. Set Cookie

1. PHP COOKIE

Cookie is a mechanism that stores data on the remote browser side and uses it 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 restrictions on the header() function.

1.1 Set cookies:

You can use the setcookie() or setrawcookie() function to set cookies. It can also be
set by sending http headers directly to the client.
1.1.1 Use the setcookie() function to set cookies:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure [, bool
httponly]] ]]]]] )
name: cookie variable name
value: cookie variable value
expire: end of validity period
path: valid directory
domain: valid domain name, unique top-level domain
secure: If the value is 1, the cookie can only be valid on https connections. If it is the default value 0, both http and https can
be valid.

Let’s look at a few 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秒=1小时

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

Everything is available:
 代码如下 复制代码

SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", ".phpuser.com", 1);

The code is as follows Copy code
SetCookie("FullCookie", "Full cookie value" , time()+3600, "/forum", ".phpuser.com", 1);

We need to use queues.

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::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 the queue is empty, first set it to an empty array
if(empty($this->server_arr))
$this->server_arr = array();
//Add data to the queue head && (count($this->server_arr) >= $this->length))
                return true;                                    
{
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)
                                                                                                                                                            $this->array_remove($this- >server_arr,$key);
                                                                                                    
        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);

复制代码


require_once("queue_svc.php");
 代码如下 复制代码
var_dump($_COOKIE);

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444690.htmlTechArticleThe article ranges from the simplest cookie operations (add, delete, modify) to our cookie queue operation class operations, Students who need to know more can refer to this example. 1. Set Cookie 1. PHP...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template