php 设置cookie与清除cookie方法
setcookie()函数定义一个cookie被发送的http头的其余部分一起,像其他头,饼干之前,必须发送从你的脚本,这是一个协议限制输出,这就需要你的地方调用这个函数之前,任何输出,包括<html>和<head>标签以及任何空格.
一旦饼干已经确定,他们可以访问下一页上加载了$ _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)
- {
- 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()
- {
- global $uid, $username, $pw, $adminid;
- makecookie('auth', '', -86400 * 365);
- $uid = $adminid = 0;
- $username = $pw = '';
- }
-
- makecookie('111','www.phpfensi.com');
-
- clearcookies();
-
- function setcookielive($name, $value='', $expire=0, $path='', $domain='', $secure=false, $httponly=false) {
-
- $_cookie[$name] = $value;
- return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
- }
-
- setcookielive('webab','111cn',time()+86000,'/','111cn.net');
-
- setcookie("cookie[three]", "cookiethree");
- setcookie("cookie[two]", "cookietwo");
- setcookie("cookie[one]", "cookieone");
-
- if (isset($_cookie['cookie'])) {
- foreach ($_cookie['cookie'] as $name => $value) {
- echo "$name : $value <br /> ";
- }
- }