Home php教程 php手册 cookie与session区别和关系

cookie与session区别和关系

Jun 13, 2016 am 09:55 AM
cookie session and about relation share the difference and detailed illustrate

分享一篇关于cookie和session区别详细说明的文章,有需要的朋友可以参考一下,很有价值的一篇文章。cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。 PHP用SetCookie函数来设置Cookie。 SetCookie函数定义了一个Cookie,并且把它附加在HTTP头的后面,SetCookie函数的原型如下:
int SetCookie(string name, string value, int expire, string path, string domain, int secure);
参数说明:cookie名称,cookie值,过期时间(int),有效路径,有限域名,https传递才有效

 代码如下 复制代码
注意:当前设置的Cookie不是立即生效的,而是要等到下一个页面时才能看到.这是由于在设置的这个页面里Cookie由服务器传递给客户浏览器,在下一个页面浏览器才能把Cookie从客户的机器里取出传回服务器的原因。
使用例子: 普通使用: setcookie('name','PHP淮北'); 带失效时间的: 
setcookie('name','PHP淮北',time()+24*60*60);//1day
Cookie是面向路径的 ,默认存储在当前文件下,如果没有设置路径,不同文件下的cookie默认保存在不同文件夹下,如图:默认保存在mytest文件夹下 phphuaibei/201111/201111151945348209.png"> 2、接收和处理Cookie 用户端与服务端的web通信协议是http。而PHP通过http取得用户数据惯用的三种方法分别是:POST方法、GET方法还有Cookie。而PHP默认传递方法正是Cookie,也是最佳方法。 比如设置一个名为MyCookier的Cookie,PHP会自动从WEB服务器接收的HTTP头里把它分析出来,并形成一个与普通变量一样的变量,名为$myCookie,这个变量的值就是Cookie的值 3,删除Cookie   要删除一个已经存在的Cookie,有两个办法:
  1. 一是调用只带有name参数的SetCookie,那么名为这个name的Cookie将被从关系户机上删掉;例如:setcookie('name','');
  2. 另一个办法是设置Cookie的失效时间为time()或time()-1,那么这个Cookie在这个页面的浏览完之后就被删除了(其实是失效了)。 例如:setcookie('name','PHP淮北',time()-24*60*60);
         要注意的是,当一个Cookie被删除时,它的值在当前页在仍然有效的。 
使用Cookie的注意事项:
    1. 首先是必须在HTML文件的内容输出之前设置(Cookie是HTTP协议头的一部分,用于浏览器和服务器之间传递信息,所以必须在任何属于HTML文件本身的内容输出之前调用Cookie函数。
在PHP页面可以先使用 ob_start();//开启 code….. ob_end_flush(); //刷新缓存 可以防止header提示错误);
  1. 不同的浏览器对Cookie的处理机制不一样
  2. cookie限制是在客户端的。一个浏览器能创建的Cookie数量最多为30个,并且每个不能超过4KB,每个WEB站点能设置的Cookie总数不能超过20个。
  3. 当前设置的Cookie不是立即生效的,而是要等到下一个页面时才能看到
Session介绍 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息,每一个网站访客都会被分配给一个唯一的标志符,即会话ID,它的存放形式无非两种:要么经过url传递,要么保存在客户端的Cookies里.当然,你也可以将Session保存到数据库里,这样会更安全,但效率方面会有所下降.url方式传递安全性肯定太差,PHP的会话机制是通过设置Cookie,在Cookie中保存会话id(Session ID),在服务器端会生成session文件,与用户进行关联,Web应用程序存储与这些Session相关的数据,并在各页面间进行传递. PHP相关函数 在PHP中有关Session的函数比较多,不过我们最常用到的也就这么几个函数: session_start():启用session机制,在需要用到session的程序文件的最开始调用它. session_register():注册session变量 session_unregister(): 删除session变量(一个一个删除) session_is_registered(): 判断session变量是否注册 session_distroy(): 销毁所有session变量(所有session变量销毁,包括文件) 需要注意下面几个方面: 1.函数session_start()必须在程序最开始执行,在其前面不能有任何输出内容,否则     就会出现“Warning:Cannot send session cookie - headers already    sent"类似这样的警告信息. 2.函数session_register()用于注册要保存在session中的相关变量,其用法如下:   $val = "session value";   session_register("val"); ?> val即为要注册的session变量名,在注册时一定不要加上"$"符号,只写其变量名称即可. 3.函数session_unregister()与上面函数用法完全相同,但功能相反,上面函数是注册 session变量,而其则是删除指定的session变量. 4.函数session_is_registered()用于判断session变量是否注册. 5.函数session_destroy()主要用于在系统注销和退出时,销毁所有的session变量,它没有参数,直接调用即可。 Session与PHP.ini的关系配置 1,session.save_handler = file 用于读取/回写session数据的方式,默认是files。它会让PHP的session管理函数使用指定的文本文件存储session数据 2,session.save_path = “/xammp/temp/”   指定保存session文件的目录,可以指定到别的目录,但是指定目录必须要有httpd守护进程属主(比如apache或www等)写权限,否则无法回存session数据。它还可以写成这样session.save_path = “N;/path” 其中N是整数。这样使得不是所有的session文件都保存在同一个目录中,而是分散在不同目录。这对于服务器处理大量session文件是很有帮助的。(注:目录需要自己手工创建) 3,session.auto_start = 0   如果启用该选项,用户的每次请求都会初始化session。不推荐使用,最好通过session_start()显示地初始化session。   上图:左侧是保存在xammp/tmp/下的session文件,内容是PHP序列化的格式 右侧:第一行是echo serialize($_SESSION['name']);//序列化 第二行是打印session值 *****************其中文件名是session-name,内容是PHP序列化的格式   cookie与session的区别和关系
  • 存储位置:
    1. session存储在服务器位置上,可以通过php.ini里面配置session相关配置
    2. cookie存储在客户端上的上(其实可以分两种:
1,持久性cookie,设置了cookie的时间,以文件方式存在硬盘上, 2,会话cookie,没有设置cookie时间,cookie的生命周期也就是关闭浏览器前就消失,一般不会保存在硬盘,而是保存在内存上) cookie和session的关系 从上面的图可以看到: cookie通过http报头发送: Cookie  <font face="Arial">name=PHP%BB%B4%B1%B1; PHPSESSID=cpt2ah3pi4cu7lo69nfbfllbo7</font> <font face="Arial" size="2">其中PHPSESSID就是关联服务器session的重要参数</font> <font face="Arial" size="2">再看session文件:sess_cpt2ah3pi4cu7lo69nfbfllbo7</font> <font face="Arial" size="2">session_id的生成格式就是:sess_加上一串PHPSESSID的值</font> <font face="Arial" size="2">我们可以这样理解:</font> 当程序需要为某个客户端的请求创建一个session时,服务器首先检查这个客户端的请求里是否已包含了一个session标识 (称为session id),如果已包含则说明以前已经为此客户端创建过session,服务器就按照session id把这个session检索出来 使用(检索不到,会新建一个),如果客户端请求不包含session id,则为此客户端创建一个session并且生成一个与此session相 关联的session id,session id的值应该是一个既不会重复,又不容易被找到规律以仿造的字符串,这个session id将被在本次响应 中返回给客户端保存。保存这个session id的方式可以采用cookie,这样在交互过程中浏览器可以自动的按照规则把这个标识发送给 服务器。一般这个cookie的名字都是类似于SEEESIONID php.ini里面关于session和cookie有关的配置 1,session.use_cookie = 1
是否采用Cookie方法传递session id值。默认是1,表示启用。
2,session.name = PHPSESSID
不管是Cookie传递sessioin_id,还是GET方法传递session_id,都需要使用键值。他们的格式分别是Cookie:  sess_name=session_id;和/path.php?sess_name=session_id,其中sess_name就是由这里指定的。
3,session.use_only_cookies = 0
表示只使用Cookie 的方法传递session id。我们说过,传递cookie的方法,除了cookie,还有GET方法,GET方法是不安全的方法。在用户端禁用了cookie的时候,会采用GET方法传递session_id,可以通过这个设置尽用GET方法传递session_id。
4,session.cookie_lifetime = 0, session.cookie_path = / 以及session.cookie_domain =
如果使用Cookie方法传递session_id的话,这里分别指定了cookie有效域、目录和时间。分别对应setcookie()函数的形参$expire、$path和$domain。其中cookie_lifetime=0表示直到关闭浏览器才删除Cookie。还可以使用session_set_cookie_params()函数修改这些值。
5,session_name([string $name])
获取或更新session_name。如果传了name,则表示不使用默认的名称PHPSESSID(由session.name)指定,否则获取当前session_name。注意:如果设置session_name,则必须在session_start()之前调用才生效。
6,session_id([string $id])
与session_name()类似,但它是读取或者设置session_id的方法。同样,设置session_id的话,必须在session_start()之前调用才有效。
7,session_set_cookie_params()和session_get_cookie_params()
通过session_set_cookie_params()可以重新设定session.cookie_lifetime, session.cookie_path以及session.cookie_domain这三个php.ini设置。而session_get_cookie_params()则是获取这些设定的值。
  总结:
  1. 服务端session的相对于客户端的cookie安全性要较高一点
  2. session在服务器集群的时候容易不同步,而cookie不会
ps:下午关于使用cookie退出出现的问题 当退出的时候使用: setcookie('username','',time()-3600);
    setcookie('name','',time()-3600);
理论上cookie应该正常清除,测试的时候发现第一登录退出完全正常,但是再次登录就是退出不了,cookie始终存在,很是郁闷,使用firebug查看原来页面设置了缓存,使用nginx设置了页面缓存,原因也就是找到了
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Does Bitcoin have stocks? Does Bitcoin have equity? Does Bitcoin have stocks? Does Bitcoin have equity? Mar 03, 2025 pm 06:42 PM

The cryptocurrency market is booming, and Bitcoin, as a leader, has attracted the attention of many investors. Many people are curious: Do Bitcoin have stocks? The answer is no. Bitcoin itself is not a stock, but investors can indirectly invest in Bitcoin-related assets through various channels, which will be explained in detail in this article. Alternatives to Bitcoin Investment: Instead of investing directly in Bitcoin, investors can participate in the Bitcoin market by: Bitcoin ETF: This is a fund traded on the stock trading market, whose asset portfolio contains Bitcoin or Bitcoin futures contracts. This is a relatively convenient option for investors who are accustomed to stock investments, without having to hold Bitcoin directly. Bitcoin Mining Company Stocks: These companies' business is Bitcoin mining and holding Bitcoin

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading Mar 03, 2025 pm 11:54 PM

In traditional financial markets, pre-market and after-market trading refers to trading activities outside the regular trading period. Although the cryptocurrency market is trading around the clock, trading platforms like Bitget also offer similar features, especially some comprehensive platforms that trade stocks and cryptocurrencies at the same time. This article will clarify the differences in pre-market and after-market trading and explore its impact on currency price. Four major differences between pre-market and after-market trading: The main differences between pre-market and after-market trading and regular trading periods are in four aspects: trading time, liquidity, price fluctuations and trading volume: Trading time: Pre-market trading occurs before the official trading starts, and after-market trading is carried out after the regular trading ends. Liquidity: The liquidity of pre- and after-hours trading is low, there are few traders, and the bid and offer price difference is large; while the liquidity is high during the regular trading period, the price is

Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Mar 04, 2025 pm 04:06 PM

Original title: Bittensor=AIBitcoin? Original author: S4mmyEth, Decentralized AI Research Original translation: zhouzhou, BlockBeats Editor's note: This article discusses Bittensor, a decentralized AI platform, hoping to break the monopoly of centralized AI companies through blockchain technology and promote an open and collaborative AI ecosystem. Bittensor adopts a subnet model that allows the emergence of different AI solutions and inspires innovation through TAO tokens. Although the AI ​​market is mature, Bittensor faces competitive risks and may be subject to other open source

Is there any difference between South Korean Bitcoin and domestic Bitcoin? Is there any difference between South Korean Bitcoin and domestic Bitcoin? Mar 05, 2025 pm 06:51 PM

The Bitcoin investment boom continues to heat up. As the world's first decentralized digital asset, Bitcoin has attracted much attention on its decentralization and global liquidity. Although China was once the largest market for Bitcoin, policy impacts have led to transaction restrictions. Today, South Korea has become one of the major Bitcoin markets in the world, causing investors to question the differences between it and its domestic Bitcoin. This article will conduct in-depth analysis of the differences between the Bitcoin markets of the two countries. Analysis of the differences between South Korea and China Bitcoin markets. The main differences between South Korea and China’s Bitcoin markets are reflected in prices, market supply and demand, exchange rates, regulatory supervision, market liquidity and trading platforms. Price difference: South Korea’s Bitcoin price is usually higher than China, and this phenomenon is called “Kimchi Premium.” For example, in late October 2024, the price of Bitcoin in South Korea was once

Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Mar 04, 2025 am 10:21 AM

Artificial intelligence agents (AIAgents) are rapidly integrating into daily operations of enterprises, from large companies to small businesses, almost all areas have begun to be used, including sales, marketing, finance, law, IT, project management, logistics, customer service and workflow automation. We are moving from an era of manual processing of data, performing repetitive tasks, and using Excel tables to an era of autonomous operation by AI agents around the clock, which not only improves efficiency but also significantly reduces costs. Application case of AI agents in Web2: YCombinator's Perspective Apten: A sales and marketing optimization tool combining AI and SMS technology. BildAI: A model that can read architectural blueprints,

Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025? Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025? Mar 03, 2025 pm 07:09 PM

After the surge in PEPE, can MUTM become a more stable investment choice in 2025? PEPE (PEPE) has made early investors profitable, but its violent price fluctuations have also made many people question its long-term prospects. As the meme currency market continues to turbulently, traders are beginning to focus on projects with more fundamental advantages, and MutuumFinance (MUTM) is one of them. This is a decentralized lending platform focusing on practical financial applications. Unlike PEPE, which relies on speculative speculation, MUTM builds a structured DeFi ecosystem where users can borrow and earn passive income. Its pre-sale has exceeded one million US dollars, the first phase of token sales rate exceeds 97%, early investment

What exchange is Nexo? Is Nexo exchange safe? What exchange is Nexo? Is Nexo exchange safe? Mar 05, 2025 pm 07:39 PM

Nexo: Not only is it a cryptocurrency exchange, but also your digital financial manager. Nexo is not a traditional cryptocurrency exchange, but a financial platform that focuses more on cryptocurrency lending. It allows users to obtain loans in cryptocurrency as collateral and provides services to earn interest. While Nexo also offers cryptocurrency buying, selling and redemption capabilities, its core business is crypto lending. This article will explore the operating model and security of Nexo in depth to provide investors with a more comprehensive understanding. Nexo's operating model was founded in 2018 and is headquartered in Zug, Switzerland, and is a pioneer in the field of digital finance. It is different from other centralized exchanges and focuses more on providing comprehensive financial services. Users can buy, sell, trade cryptocurrencies without selling assets and

The difference between Ether and Bitcoin What is the difference between Ether and Bitcoin The difference between Ether and Bitcoin What is the difference between Ether and Bitcoin Mar 19, 2025 pm 04:54 PM

The difference between Ethereum and Bitcoin is significant. Technically, Bitcoin uses PoW, and Ether has shifted from PoW to PoS. Trading speed is slow for Bitcoin and Ethereum is fast. In application scenarios, Bitcoin focuses on payment storage, while Ether supports smart contracts and DApps. In terms of issuance, the total amount of Bitcoin is 21 million, and there is no fixed total amount of Ether coins. Each security challenge is available. In terms of market value, Bitcoin ranks first, and the price fluctuations of both are large, but due to different characteristics, the price trend of Ethereum is unique.

See all articles