


PHP queue operation class example for cookie operation, cookie queue_PHP tutorial
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:
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:
With expiration time. The code is as follows:
Everything is available, the code is as follows:
We need to use a queue, the code is as follows:
{/*{{{*/
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("cookie_svc.php");
$cookie_id = '4';
CookieSvc::set($cookie_id);
这样就可以了,大家可以每次把$cookie_id换做不同的值,来检验此操作,检验的代码可以用如下代码:
二、常见问题解决:
1. 用 setcookie()时有错误提示,可能是因为调用setcookie()前面有输出或空格。也可能你的文档是从其他字符集转换过来,文档后面可能带有 BOM 签名(就是在文件内容添加一些隐藏的BOM 字符),解决的办法就是使你的文档不出现这种情况,还有通过使用ob_start()函数也能处理一点.
2. $_COOKIE 受magic_quotes_gpc 影响,可能自动转义.
3. 使用的时候,有必要测试用户是否支持cookie.
希望本文所述对大家的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
