Encapsulation of the PHP session cookie Class

Source: Internet
Author: User
Tags php session setcookie

<?php
Header (' Content-type:text/html;charset=utf-8 ');
/**
* Complete the setting, deletion, updating and reading of cookies
*/
Class cookie{
private static $instance =null;
Private $expire =0;//expiration unit is s default is session Close browser is not present
Private $path = ';//the path is valid in this directory and sub-directory in the active/indicated root directory
Private $domain = ";//Domain
Private $secure =false;//Whether the default is set only under the HTTPS protocol is not
Private $httponly =false;//If true, cookies can only be accessed through the HTTP protocol. This means that the scripting language (such as JavaScript) cannot access cookies
/**
* [__construct description]
* constructor to complete the initialization of cookie parameters
* @DateTime 2018-07-25t09:50:51+0800
* @param array $options [cookie-related options]
*/
Private function __construct (array $options =[]) {
$this->getoptions ($options);
}
Private function GetOptions (array $options =[]) {
if (Isset ($options [' expire '])) {$this->expire= $options [' expire '];}
if (Isset ($options [' path ')) $this->path= $options [' Path '];
if (Isset ($options [' domain ')]) $this->domain= $options [' Domain '];
if (Isset ($options [' secure ')]) $this->secure= $options [' secure '];
if (Isset ($options [' HttpOnly ')) $this->httponly= $options [' HttpOnly '];
return $this;
}
/**
* [getinstance description]
* Single case mode
* @DateTime 2018-07-25t09:50:01+0800
* @param array $options [cookie-related options]
* @return Object $options [Objects instance]
*/
public static function getinstance (array $options =[]) {
if (Is_null (self:: $instance)) {
$class =__class__;
Self:: $instance =new $class ($options);
}
Return self:: $instance;
}
/**
* Set Cookies
* Func Description
* @DateTime 2018-07-25t09:42:37+0800
* @param [string] $name [cookie name]
* @param [mixed] $vlaue [cookie value]
* @param array $options [cookie-related options]
*/
Public function set ($name, $value, array $options =[]) {
if (Is_array ($options) &&count ($options) >0) {
$this->getoptions ($options);
}
if (Is_array ($value) | | Is_object ($value)) {
$value =json_encode ($value);
Var_dump ($value);

}
Setcookie ($name, $value, $this->expire, $this->path, $this->domain, $this->secure, $this->httponly);
}
/**
* [Get description]
* Read Cookie value
* @DateTime 2018-07-25t11:34:04+0800
* @param [string] $name [cookie name]
* @return [mixed] [value in array form or single value]
*/
Public function Get ($name) {
$value =$_cookie[$name];
if (Is_array ($value)) {
$arr =[];
foreach ($value as $k = = $v) {
# code ...
$arr [$k]=substr ($v, 0,1) = = ' {'? Json_decode ($value): $v;
}
return $arr;
}else{
Return substr ($value, 0,1) = = ' {'? Json_decode ($value): $value;}
}
/**
* [Delete description]
* Delete the corresponding cookie
* @DateTime 2018-07-25t11:53:24+0800
* @param [string] $name [cookie name can be an array]
* @param array $options [cookie-related parameters]
* @return [Type] [description]
*/
Public Function Delete ($name, array $options =[]) {
if (Is_array ($options) &&count ($options) >0) {
$this->getoptions ($options);
}
$value =$_cookie[$name];
if ($value) {
if (Is_array ($value)) {

foreach ($value as $k = = $v) {
# code ...
Setcookie ($name. ' ['. $k. '] ', ', ', Time ()-1, $this->path, $this->domain, $this->secure, $this->httponly);
Unset ($v);
}
}else{
Setcookie ($name, ", Time ()-1, $this->path, $this->domain, $this->secure, $this->httponly);
Unset ($value);
}
}
}

}
$cookie =cookie::getinstance ();
$cookie->set (' aaaa ', ' BB ');
$cookie->set (' BB ', ' BB ', [' Expire ' =>time () +3600, ' path ' = = '/', ' domain ' = ' localhost ', ' secure ' = False, ' HttpOnly ' =>true]);
$cookie->set (' admin ', [' name ' = ' Zhang San ', ' age ' =>20]);
$cookie->set (' user[name] ', ' John Doe ');
$cookie->set (' user[age] ', 25);
Var_dump ($_cookie[' user ');
$cookie->delete (' user ');


If there is not a comprehensive place, you are welcome to correct me.

Encapsulation of the PHP session cookie Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.