This article is an example of how cookies in PHP can achieve two-level domain-accessible operations. Share to everyone for your reference. The specific methods are as follows:
Cookies are commonly used in some applications, assuming that I have a multi-level domain name requirements can also access the main domain binding cookies, the following to give you a specific introduction in PHP in the use of Setcookie to achieve a two domain name can successfully access the main domain name cookie value method.
Sometimes two domain names may be on different servers, but we still hope that the level two domain name can successfully access the main domain cookie, the main domain name can be successfully access to the two-level domain name cookies, such as sc.jb51.net want to access www.jb51.net and Cookies for Blog.jb51.net
Here are 3 ways you can often hear global cookie settings.
The first instance code is as follows:
Copy Code code as follows:
Setcookie ("jb51", $s, Time () +3600*12, '/', ' *.jb51.net ');
* Number cannot successfully set a cookie
The second type of instance code is as follows:
Copy Code code as follows:
Setcookie ("jb51", $s, Time () +3600*12, '/', '. jb51.net ');
Successfully set a global cookie so that it can be read correctly under Ss.jb51.net
The third type of instance code is as follows:
Copy Code code as follows:
Setcookie ("jb51", $s, Time () +3600*12, '/', ' jb51.net ');
Successfully set a global cookie and read it correctly under Ss.jb51.net
This way of understanding is only jb51.net can read, under the Firefox test success, ie under test success, the code is as follows:
Copy Code code as follows:
Setcookie ("jb51", $s, Time () +3600*12, '/', ' ss.jb51.net ');
Set up a cookie that can be read correctly only under the Ss.jb51.net domain name, and the standard on the web is. Jb51.net So, there is also the idea of * (which is completely wrong). Here is a good PHP cookie action class, you can set cookies, Get the cookie, delete the cookie, and the code is as follows:
Copy Code code as follows:
<?php
/**
* PHP Cookie Class
* Class:php_cookie
*/
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 (Emptyempty ($cname) && isset ($this)) {
$cname = $this->_name;
}
if (!emptyempty ($_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;
}
To remove cookies from the global scope
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 (Emptyempty ($value)) {
unset ($this->_val[$var]);
}
}
function Clear ()
{
$this->_val=array ();
}
function set ()
{
if (Emptyempty ($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);
}
}
?>
I hope this article will help you with your PHP program design.