>  기사  >  백엔드 개발  >  PHP 쿠키 클래스(쿠키 값 설정, 가져오기, 삭제)

PHP 쿠키 클래스(쿠키 값 설정, 가져오기, 삭제)

WBOY
WBOY원래의
2016-07-25 08:56:341260검색
本文介绍下,一个功能强大的php cookie操作类,可以完成设置cookie、获取cookie值、删除cookie值等操作,有需要的朋友参考下。

分享一个php cookie操作的类,可以设置cookie、获取cookie、删除cookie。

代码:

<?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); 
  } 
} 
?>

调用示例: 1,设置cookie

<?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";
?>

2,获取cookie

<?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";
?>

3,删除cookie

<?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();
?>


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.