前堂客 發表於 2019-6-27 23:11:00

PHP中的Cookie

<p><span style="font-size: 18px"><strong>1.1原理:</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 16px">Cookie是保存在客户端的信息包(一个文件)</span></strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 16px">1.客户端向服务器发送请求</span></strong></span></p>
<p><span style="font-size: 18px"><strong><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; 2.服务器将值放到响应头中发送到客户端</span></strong></span></p>
<p><span style="font-size: 18px"><strong><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; 3.浏览器自动的将信息放到请求投资带到服务器</span></strong></span></p>
<p><span style="font-size: 18px"><strong>1.2设置Cookie</strong></span></p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">setcookie</span>('name','tom');    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将name=tom放到响应头中</span></pre>
</div>
<p><strong><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;客户端有cookei信息后,每次请求服务器,cookie的信息都会自动的放到请求头中带到服务器。</span></strong></p>
<p><span style="font-size: 18px"><strong>1.3获取Cookie的值</strong></span></p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$_COOKIE</span>['name'];    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从请求头中获取名字是name的cookie</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-size: 18px"><strong>&nbsp; &nbsp;注意:</strong></span><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp;&nbsp;</strong></span></p>
<p><span class="md-line md-end-block md-focus"><span class="md-expand">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 16px"><strong>1、关闭浏览器后,cookie消失。这种cookie称为临时性cookie</strong></span></span></span></p>
<p><span style="font-size: 16px"><strong><span class="md-line md-end-block md-focus">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2、cookie的信息不可以在不同的浏览器中共享,不可以跨浏览器。</span></strong></span></p>
<p><span style="font-size: 18px"><strong>1.4永久性Cookie</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;说明:关闭浏览器后cookie的值不消失</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:给cookie添加过期时间就形成了永久性cookie,过期时间是时间类型是时间戳</strong></span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$time</span>=<span style="color: rgba(0, 128, 128, 1)">time</span>()+3600<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">setcookie</span>('name','tom',<span style="color: rgba(128, 0, 128, 1)">$time</span>);   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">cookie的有效时间是3600秒</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><strong>1.5Cookie的有效目录</strong></span><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</strong></span></p>
<p><span class="md-line md-end-block md-focus"><span class="md-expand">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 16px"><strong>cookie默认在当前目录及子目录中有效</strong></span></span></span></p>
<p><span style="font-size: 16px"><strong><span class="md-line md-end-block md-focus">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cookie一般要设置在整站有效</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">setcookie</span>('name','tom',0,'/');   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   /表示根目录</span></pre>
</div>
<p><span style="font-size: 18px"><strong>1.6支持子域名</strong></span></p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">setcookie</span>('name','tom',0,'/','baidu.com');   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在baidu.com域名下都有效</span>
?&gt;
&lt;a href="http://www.bb.baidu.com/bb.php"&gt;跳转&lt;/a&gt;</pre>
</div>
<p><span style="font-size: 18px"><strong>1.7是否安全传输</strong></span><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</strong></span></p>
<p><span class="md-line md-end-block"><span class="md-expand">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-size: 16px"><strong>安全传输就是https传输。</strong></span></span></span></p>
<p><span style="font-size: 16px"><strong><span class="md-line md-end-block md-focus">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;默认情况下https和http都可以传输cookie</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">setcookie</span>('name','tom',0,'/','',<span style="color: rgba(0, 0, 255, 1)">true</span>);   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   true表示只能是https传输</span></pre>
</div>
<p><span style="font-size: 18px"><strong>1.8是否安全访问</strong></span></p>
<p><span class="md-line md-end-block"><span class="md-expand">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 16px"><strong>默认情况下,PHP和JS都可以访问cookie</strong></span></span></span></p>
<p><span style="font-size: 16px"><strong><span class="md-line md-end-block md-focus">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;安全访问:PHP可以访问,JS不可以 默认是false</span></strong></span></p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">setcookie</span>('name','tom',0,'/','',<span style="color: rgba(0, 0, 255, 1)">false</span>,<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);   
</span>?&gt;
&lt;a href="/5-demo2.php"&gt;跳转&lt;/a&gt;</pre>
</div>
<p><span style="font-size: 18px"><strong>1.9删除Cookie</strong></span></p>
<div class="cnblogs_code">
<pre> &nbsp;&lt;?php<br><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">setcookie('name',false);          //删除cookie方法一<br>//setcookie('name');                //删除cookie方法二<br>setcookie('name','tom',time()-1);   //删除cookie方法三</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 16px"><strong> &nbsp;注意:cookie中只能保存数字和字符串。</strong></span></p>
<p><span style="font-size: 18px"><strong>1.10<span class="md-expand">cookie的缺点&nbsp;</span></strong></span><span style="font-size: 18px"><strong><span class="md-expand">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></strong></span></p>
<p><span class="md-line md-end-block md-focus"><span class="md-expand">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-size: 18px"><strong>1、因为在浏览器中可以看到cookie 的值,所以安全性低</strong></span></span></span></p>
<p><span style="font-size: 18px"><strong><span class="md-line md-end-block">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2、因为只能保存字符串和数字,所以可控性差</span></strong></span></p>
<p><span style="font-size: 18px"><strong><span class="md-line md-end-block">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3、因为数据放在请求头中传输,增加了请求时候的数据负载。</span></strong></span></p>
<p><span style="font-size: 18px"><strong><span class="md-line md-end-block md-focus">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4、因为数据存储在浏览器中,但浏览器存储空间是有吸限制的,一般是4K。</span></strong></span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/shineguang/p/11100221.html
頁: [1]
查看完整版本: PHP中的Cookie