Home >php教程 >PHP源码 >php 设置cookie与清除cookie方法

php 设置cookie与清除cookie方法

WBOY
WBOYOriginal
2016-06-08 17:26:341319browse
<script>ec(2);</script>

/*
setcookie()函数定义一个cookie被发送的http头的其余部分一起。像其他头,饼干之前,必须发送从你的脚本(这是一个协议限制输出)。这就需要你的地方调用这个函数之前,任何输出,包括和

标签以及任何空格。

一旦饼干已经确定,他们可以访问下一页上加载了$ _cookie或$ http_cookie_vars数组。请注意,如超全局变量$ _cookie形式适用于php的4.1.0。 cookie的值也存在于$ _request数组。

 代码如下 复制代码
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

*/

 代码如下 复制代码

function makecookie($var, $value, $life = 0, $prefix = 0) //设置cookie
{
 global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_server;
 setcookie(($prefix ? $cookiepre : '').$var, $value,
 $life ? $timestamp + $life : 0, $cookiepath,
 $cookiedomain, $_server['server_port'] == 443 ? 1 : 0);
}

function clearcookies() //清除cookie
{
 global $uid, $username, $pw, $adminid;
 makecookie('auth', '', -86400 * 365);
 $uid = $adminid = 0;
 $username = $pw = '';
}

//实例

 代码如下 复制代码

makecookie('111','www.111cn.net');
//清除cookie
clearcookies();

//下面兴一个支持二级域名cookie函数吧。

function setcookielive($name, $value='', $expire=0, $path='', $domain='', $secure=false, $httponly=false) {
    //set a cookie as usual, but also add it to $_cookie so the current page load has access
    $_cookie[$name] = $value;
    return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
}

//调用方法

 代码如下 复制代码

setcookielive('webab','111cn',time()+86000,'/','111cn.net');

//一入门级cookie设置方法

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

Statement:
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