国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Cookies operation class instance implemented by php, phpcookies class instance
How to set multiple values ??for cookies in php? Just like asp, you can set multiple values ??
How to read COOKIES in PHP
Home Backend Development PHP Tutorial Cookies operation class instance implemented by php, phpcookies class instance_PHP tutorial

Cookies operation class instance implemented by php, phpcookies class instance_PHP tutorial

Jul 13, 2016 am 10:18 AM
cookies php

Cookies operation class instance implemented by php, phpcookies class instance

The examples in this article describe the Cookies operation class implemented by PHP and its usage, and share it with everyone for your reference. The specific analysis is as follows:

1. Function:

1. Save, read, update, and clear cookie data.
2. Prefix can be set.
3. Forced timeout control.
4.Cookies data can be strings, arrays, objects, etc.

2. Usage:

Cookies.class.php class file is as follows:

<&#63;php 
/** Cookies class 保存,讀取,更新,清除cookies數(shù)據(jù)。可設(shè)置前綴。強制超時。數(shù)據(jù)可以是字符串,數(shù)組,對象等。 
*  Date:  2013-12-22 
*  Author: fdipzone 
*  Ver:  1.0 
* 
*  Func: 
*  public  set    設(shè)置cookie 
*  public  get    讀取cookie 
*  public  update   更新cookie 
*  public  clear   清除cookie 
*  public  setPrefix 設(shè)置前綴 
*  public  setExpire 設(shè)置過期時間 
*  private authcode  加密/解密 
*  private pack    將數(shù)據(jù)打包 
*  private unpack   將數(shù)據(jù)解包 
*  private getName  獲取cookie name,增加prefix處理 
*/ 
 
class Cookies{ // class start 
 
  private $_prefix = '';                         // cookie prefix 
  private $_securekey = 'ekOt4_Ut0f3XE-fJcpBvRFrg506jpcuJeixezgPNyALm';  // encrypt key 
  private $_expire = 3600;                        // default expire 
 
  /** 初始化 
  * @param String $prefix   cookie prefix 
  * @param int  $expire   過期時間 
  * @param String $securekey cookie secure key 
  */ 
  public function __construct($prefix='', $expire=0, $securekey=''){ 
 
    if(is_string($prefix) && $prefix!=''){ 
      $this->_prefix = $prefix; 
    } 
 
    if(is_numeric($expire) && $expire>0){ 
      $this->_expire = $expire; 
    } 
 
    if(is_string($securekey) && $securekey!=''){ 
      $this->_securekey = $securekey; 
    } 
 
  } 
 
  /** 設(shè)置cookie 
  * @param String $name  cookie name 
  * @param mixed $value cookie value 可以是字符串,數(shù)組,對象等 
  * @param int  $expire 過期時間 
  */ 
  public function set($name, $value, $expire=0){ 
 
    $cookie_name = $this->getName($name); 
    $cookie_expire = time() + ($expire&#63; $expire : $this->_expire); 
    $cookie_value = $this->pack($value, $cookie_expire); 
    $cookie_value = $this->authcode($cookie_value, 'ENCODE', $this->_securekey); 
 
    if($cookie_name && $cookie_value && $cookie_expire){ 
      setcookie($cookie_name, $cookie_value, $cookie_expire); 
    } 
 
  } 
 
  /** 讀取cookie 
  * @param String $name  cookie name 
  * @return mixed     cookie value 
  */ 
  public function get($name){ 
 
    $cookie_name = $this->getName($name); 
 
    if(isset($_COOKIE[$cookie_name])){ 
 
      $cookie_value = $this->authcode($_COOKIE[$cookie_name], 'DECODE', $this->_securekey); 
      $cookie_value = $this->unpack($cookie_value); 
 
      return isset($cookie_value[0])&#63; $cookie_value[0] : null; 
 
    }else{ 
      return null; 
    } 
 
  } 
 
  /** 更新cookie,只更新內(nèi)容,如需要更新過期時間請使用set方法 
  * @param String $name  cookie name 
  * @param mixed $value cookie value 
  * @return boolean 
  */ 
  public function update($name, $value){ 
 
    $cookie_name = $this->getName($name); 
 
    if(isset($_COOKIE[$cookie_name])){ 
 
      $old_cookie_value = $this->authcode($_COOKIE[$cookie_name], 'DECODE', $this->_securekey); 
      $old_cookie_value = $this->unpack($old_cookie_value); 
 
      if(isset($old_cookie_value[1]) && $old_cookie_vlaue[1]>0){ // 獲取之前的過期時間 
 
        $cookie_expire = $old_cookie_value[1]; 
 
        // 更新數(shù)據(jù) 
        $cookie_value = $this->pack($value, $cookie_expire); 
        $cookie_value = $this->authcode($cookie_value, 'ENCODE', $this->_securekey); 
 
        if($cookie_name && $cookie_value && $cookie_expire){ 
          setcookie($cookie_name, $cookie_value, $cookie_expire); 
          return true; 
        } 
      } 
    } 
    return false; 
  } 
 
  /** 清除cookie 
  * @param String $name  cookie name 
  */ 
  public function clear($name){ 
 
    $cookie_name = $this->getName($name); 
    setcookie($cookie_name); 
  } 
 
  /** 設(shè)置前綴 
  * @param String $prefix cookie prefix 
  */ 
  public function setPrefix($prefix){ 
 
    if(is_string($prefix) && $prefix!=''){ 
      $this->_prefix = $prefix; 
    } 
  } 
 
  /** 設(shè)置過期時間 
  * @param int $expire cookie expire 
  */ 
  public function setExpire($expire){ 
 
    if(is_numeric($expire) && $expire>0){ 
      $this->_expire = $expire; 
    } 
  } 
 
  /** 獲取cookie name 
  * @param String $name 
  * @return String 
  */ 
  private function getName($name){ 
    return $this->_prefix&#63; $this->_prefix.'_'.$name : $name; 
  } 
 
  /** pack 
  * @param Mixed $data   數(shù)據(jù) 
  * @param int  $expire  過期時間 用于判斷 
  * @return 
  */ 
  private function pack($data, $expire){ 
 
    if($data===''){ 
      return ''; 
    } 
 
    $cookie_data = array(); 
    $cookie_data['value'] = $data; 
    $cookie_data['expire'] = $expire; 
    return json_encode($cookie_data); 
  } 
 
  /** unpack 
  * @param Mixed $data 數(shù)據(jù) 
  * @return       array(數(shù)據(jù),過期時間) 
  */ 
  private function unpack($data){ 
 
    if($data===''){ 
      return array('', 0); 
    } 
 
    $cookie_data = json_decode($data, true); 
 
    if(isset($cookie_data['value']) && isset($cookie_data['expire'])){ 
 
      if(time()<$cookie_data['expire']){ // 未過期 
        return array($cookie_data['value'], $cookie_data['expire']); 
      } 
    } 
    return array('', 0); 
  } 
 
  /** 加密/解密數(shù)據(jù) 
  * @param String $str    原文或密文 
  * @param String $operation ENCODE or DECODE 
  * @return String      根據(jù)設(shè)置返回明文活密文 
  */ 
  private function authcode($string, $operation = 'DECODE'){ 
 
    $ckey_length = 4;  // 隨機(jī)密鑰長度 取值 0-32; 
 
    $key = $this->_securekey; 
 
    $key = md5($key); 
    $keya = md5(substr($key, 0, 16)); 
    $keyb = md5(substr($key, 16, 16)); 
    $keyc = $ckey_length &#63; ($operation == 'DECODE' &#63; substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
 
    $cryptkey = $keya.md5($keya.$keyc); 
    $key_length = strlen($cryptkey); 
 
    $string = $operation == 'DECODE' &#63; base64_decode(substr($string, $ckey_length)) : sprintf('%010d', 0).substr(md5($string.$keyb), 0, 16).$string; 
    $string_length = strlen($string); 
 
    $result = ''; 
    $box = range(0, 255); 
 
    $rndkey = array(); 
    for($i = 0; $i <= 255; $i++) { 
      $rndkey[$i] = ord($cryptkey[$i % $key_length]); 
    } 
 
    for($j = $i = 0; $i < 256; $i++) { 
      $j = ($j + $box[$i] + $rndkey[$i]) % 256; 
      $tmp = $box[$i]; 
      $box[$i] = $box[$j]; 
      $box[$j] = $tmp; 
    } 
 
    for($a = $j = $i = 0; $i < $string_length; $i++) { 
      $a = ($a + 1) % 256; 
      $j = ($j + $box[$a]) % 256; 
      $tmp = $box[$a]; 
      $box[$a] = $box[$j]; 
      $box[$j] = $tmp; 
      $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); 
    } 
 
    if($operation == 'DECODE') { 
      if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { 
        return substr($result, 26); 
      } else { 
        return ''; 
      } 
    } else { 
      return $keyc.str_replace('=', '', base64_encode($result)); 
    } 
  } 
} // class end 
 
&#63;> 

demo.php sample program is as follows:

<&#63;php 
require 'Cookies.class.php'; 
 
$type = isset($_GET['type'])&#63; strtolower($_GET['type']) : ''; 
 
if(!in_array($type, array('set','get','update','clear'))){ 
  exit('type not exists'); 
} 
 
$obj = new Cookies('member', 10); // obj 
 
switch($type){ 
 
  case 'set': // 設(shè)置 
    $data = array( 
      'name' => 'fdipzone', 
      'gender' => 'male' 
    ); 
    $obj->set('me', $data, 5); 
    echo 'set cookies'; 
    break; 
 
  case 'get': // 讀取 
    $result = $obj->get('me'); 
 
    echo '<pre class="brush:php;toolbar:false">'; 
    print_r($result); 
    echo '
'; echo 'get cookies'; break; case 'update': // 更新 $data = array( 'name' => 'angelababy', 'gender' => 'female' ); $flag = $obj->update('me', $data); if($flag){ echo 'update cookies success'; }else{ echo 'update cookies false'; } break; case 'clear': // 清除 $obj->clear('me'); echo 'clear cookies'; break; } ?>

Click here to download the complete example source code of this article.

I hope this article will be helpful to everyone’s PHP programming design.

How to set multiple values ??for cookies in php? Just like asp, you can set multiple values ??

// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {?
echo "$name : $value
\n";
}
}
?>

Examples in the manual

How to read COOKIES in PHP

[IT168 Technical Documentation] Cookies must be assigned a value before the server sends any content to the client's browser. To do this, the cookie settings must be placed in the < HEAD tag: < ?phpsetcookie("CookieID", $USERID);?< HTML< BODY< /BODY< /HTML There are six setcookie functions in total Parameters, separated by commas:
The name of the cookie, which is a string, for example: "CookieID". Colons, commas and spaces are not allowed in between. This parameter is required, while all other parameters are optional. If only this parameter is given, the cookie will be deleted.
The value of cookie, usually a string variable, for example: $USERID. You can also assign a ? to it to skip setting the value.
The time when the cookie expires. If omitted (or assigned a value of zero), the cookie will expire at the end of this session. This parameter can be an absolute time, represented by DD-Mon-YY HH:MM:SS, such as: "24-Nov-99 08:26:00". What is more commonly used is to set a relative time. This is achieved through the time() function or the mktime function. For example, time()+3600 will cause the cookie to expire after one hour.
A path used to match cookies. When there are multiple cookie settings with the same name on a server, this parameter is used to avoid confusion. Using the "/" path has the same effect as omitting this parameter. It should be noted that Netscape's cookie definition puts the domain name in front of the path, while PHP does the opposite.
The domain name of the server is also used to match cookies. It should be noted that a dot (.) must be placed before the domain name of the server. For example: ".friendshipcenter.com" . Because unless there are more than two points, this parameter cannot be accepted.
The security level of the cookie is an integer. 1 means this cookie can only be sent over "secure" networks. 0 or omitted means any type of network is acceptable

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/883685.htmlTechArticleCookies operation class examples implemented by php, phpcookies class examples This article describes the Cookies operation class implemented by PHP and its usage , shared with everyone for your reference. The specific analysis is as follows: 1....
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

How to Install PHP on Windows How to Install PHP on Windows Jul 15, 2025 am 02:46 AM

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

PHP Syntax: The Basics PHP Syntax: The Basics Jul 15, 2025 am 02:46 AM

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

PHP 8 Installation Guide PHP 8 Installation Guide Jul 16, 2025 am 03:41 AM

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

What is PHP and What is it Used For? What is PHP and What is it Used For? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

Your First PHP Script: A Practical Introduction Your First PHP Script: A Practical Introduction Jul 16, 2025 am 03:42 AM

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

How Do You Handle File Operations (Reading/Writing) in PHP? How Do You Handle File Operations (Reading/Writing) in PHP? Jul 16, 2025 am 03:48 AM

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

See all articles