Cookie简介 随笔

Cookie是服务器i留在用户计算机中的小文件.每当相同的计算机通过过浏览器请求页面时,它同时会发送cookie ,您能够创建并取回Cookie的值.

1. php set cookie 

 function setCookie(cookie_name,cookie_value,expire_name,path,domain)

Attention :

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace. 

大概意思:首先是必须在HTML文件的内容输出之前设置(Cookie是HTTP协议头的一部分,用于浏览器和服务器之间传递信息,所以必须在任何属于HTML文件本身的内容输出之前调用Cookie函数。

输出函数之后不能使用setCookie

同样要注意的函数有:ob_start等. 具体原理可以去搜索编程的三种缓存:浏览器缓存,程序缓存,服务器内存缓存等.

About the function description And Parameters  :

 

name :Cookie's Key
The name of the cookie. 
value 
The value of the cookie. This value is stored on the clients computer; do not store sensitive information. Assuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename'] 
expire 
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes). 
Note: 
You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally. 
path 
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in. 
domain 
The (sub)domain that the cookie is available to. Setting this to a subdomain (such as 'www.example.com') will make the cookie available to that subdomain and all other sub-domains of it (i.e. w2.www.example.com). To make the cookie available to the whole domain (including all subdomains of it), simply set the value to the domain name ('example.com', in this case). 
Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains. 
secure 
Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. On the server-side, it's on the programmer to send this kind of cookie only on secure connection (e.g. with respect to $_SERVER["HTTPS"]). 
httponly 
When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript. It has been suggested that this setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers), but that claim is often disputed. Added in PHP 5.2.0. TRUE or FALSE 
Return Values
If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie. 

  Example :

  setCookie("new","dc");

接受和处理cookie 

 用户端与服务器端的web通信协议是http.而php通常通过http 取得用户数据的三种方法:

1.GET 2.POST 3 .cookie($_COOKIE['key']) ,php默认的传递方式正式cookie

2. 删除cookie

   方法案例: setCookie('name','');  

   方法案例:setCookie('name','',time()-1)

    注意:当一个cookie 被删除的时候,它的值在当前页面仍然有效的.

   案例:

    

setCookie("new","dc",time()+24*60*60);
setCookie("new","dc",time()-1);
$res=$_COOKIE['new'];
echo "<pre>";
print_r($res);

  1.不同的浏览器对Cookie的处理机制不一样,限制于浏览器,Cookie的数量最多为30个,并且不能超过4KB,每个站点设置的数量不能超过20个左右

Session的介绍:

   是服务器端的功能.类似于散列表的结构来保存信息.  每个网站的访客都被分配一个标识性的会话ID,存放有两种情况,一种通过url来传递,保存在cookie中传递[高并发下,taobao的就是这种解决方案,session ID过多,会有一个命中率的问题]. session可以存放的地方很多,数据库,硬盘,缓存[redis,memcache]等存储数据库.服务器会自动生成一个session文件和用户关联,而关联的就是sessionID

.PHP 中session的函数:

session_start() ,使用注意事项同setCookie

 

 

  

posted @ 2017-10-20 23:33  -起风了🪐  阅读(205)  评论(0编辑  收藏  举报