php cookie class (set, get, delete cookie value)
This article introduces a powerful php cookie operation class that can complete operations such as setting cookies, obtaining cookie values, and deleting cookie values. Friends in need can refer to it.
Share a php cookie operation class, which can set cookies, obtain cookies, and delete cookies. Code: <?php /** * php cookie类 * class:PHP_COOKIE * by bbs.it-home.org */ class PHP_COOKIE { var $_name = ""; var $_val = array(); var $_expires; var $_dir = '/';// all dirs var $_site = ''; function PHP_COOKIE($cname, $cexpires="", $cdir="/", $csite="") { $this->_name=$cname; if($cexpires){ $this->_expires=$cexpires; } else{ $this->_expires=time() + 60*60*24*30*12; // ~12 months } $this->_dir=$cdir; $this->_site=$csite; $this->_val=array(); $this->extract(); } function extract($cname="") { if(!isset($_COOKIE)){ global $_COOKIE; $_COOKIE=$GLOBALS["HTTP_COOKIE_VARS"]; } if(empty($cname) && isset($this)){ $cname=$this->_name; } if(!empty($_COOKIE[$cname])){ if(get_magic_quotes_gpc()){ $_COOKIE[$cname]=stripslashes($_COOKIE[$cname]); } $arr=unserialize($_COOKIE[$cname]); if($arr!==false && is_array($arr)){ foreach($arr as $var => $val){ $_COOKIE[$var]=$val; if(isset($GLOBALS["PHP_SELF"])){ $GLOBALS[$var]=$val; } } } if(isset($this)) $this->_val=$arr; } // 在全局范围内移除cookie unset($_COOKIE[$cname]); unset($GLOBALS[$cname]); } function put($var, $value) { $_COOKIE[$var]=$value; $this->_val["$var"]=$value; if(isset($GLOBALS["PHP_SELF"])){ $GLOBALS[$var]=$value; } if(empty($value)){ unset($this->_val[$var]); } } function clear() { $this->_val=array(); } function set() { if(empty($this->_val)){ $cookie_val=""; } else { $cookie_val=serialize($this->_val); } if(strlen($cookie_val)>4*1024){ trigger_error("The cookie $this->_name exceeds the specification for the maximum cookie size. Some data may be lost", E_USER_WARNING); } setcookie("$this->_name", $cookie_val, $this->_expires, $this->_dir, $this->_site); } } ?> Copy after login Call example: 1. Set cookies <?php //cookie操作类 include("class.cookie.php"); // Create a local object $PHP_COOKIE=new PHP_COOKIE("test_cookie"); // Add the variables to be saved in the cookie $PHP_COOKIE->put("namefirst","Jo"); $PHP_COOKIE->put("namelast","Foo"); $PHP_COOKIE->put("number","1234"); $PHP_COOKIE->put("time",time()); // Set the cookie $PHP_COOKIE->set(); $PHP_COOKIE=new PHP_COOKIE("test_cookie 123"); // Add the variables to be saved in the cookie $PHP_COOKIE->put("namefirst","Jo123"); $PHP_COOKIE->put("namelast","Foo13"); $PHP_COOKIE->put("number","123413"); // Set the cookie $PHP_COOKIE->set(); echo "<br>The values saved in the cookie test_cookie are:"; echo "<br>namefirst: = $_COOKIE[namefirst]"; echo "<br>namelast: = $_COOKIE[namelast]"; echo "<br>number: = $_COOKIE[number]"; echo "<br>time: = $_COOKIE[time]"; echo "<br><br>END"; ?> Copy after login 2. Get cookies <?php include("class.cookie.php"); //获取cookie //从保存的cookie中解析变量,然后加入自己的cookies中 PHP_COOKIE::extract("test_cookie"); //显示cookie echo "<BR>显示用于测试的一些cookie值" ; echo "<br> Name: "; echo $_COOKIE['namefirst']; echo " "; echo $_COOKIE['namelast']; echo "<br> Number: "; echo $_COOKIE['number']; echo "<br> Time: "; echo $_COOKIE['time']; echo "<br><br>END"; ?> Copy after login 3. Delete cookies <?php include("class.cookie.php"); //删除cookie //方法1. 设置cookie过期时间 //方法2. 调用 clear()与 set()方法 // Create a local object $PHP_COOKIE=new PHP_COOKIE("test_cookie", time()-86400); // Set the cookie $PHP_COOKIE->set(); // Clear all values #$PHP_COOKIE->clear(); ?> Copy after login |

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

AI Hentai Generator
Generate AI Hentai for free.

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



Alipay PHP...

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,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
