PHP的COOKIE在设定之后,必须要刷新一下网页才能生效,至于是什么原因,有人说是为了安全考虑,至于你信不信,反正我信了。
不过,这种机制总是会给我们带来这或那的问题,比如前段时间,我的站点上得设置一个广告显示的功能,就需要用到COOKIE,主要目的是为了加大广告的转化率,可是如果刷新生效,就比较麻烦,所以就用了这种方法,好用。
下面是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span style="color: #000000;font-weight: bold">function</span> cookie<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span> <span style="color: #000088">$value</span><span style="color: #339933">=</span><span style="color: #0000ff">''</span><span style="color: #339933">,</span> <span style="color: #000088">$time</span><span style="color: #339933">=</span><span style="color: #cc66cc">0</span><span style="color: #339933">,</span> <span style="color: #000088">$path</span><span style="color: #339933">=</span><span style="color: #0000ff">''</span><span style="color: #339933">,</span> <span style="color: #000088">$domain</span><span style="color: #339933">=</span><span style="color: #0000ff">''</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
<span style="color: #000088">$_COOKIE</span><span style="color: #009900">[</span><span style="color: #000088">$var</span><span style="color: #009900">]</span> <span style="color: #339933">=</span> <span style="color: #000088">$value</span><span style="color: #339933">;</span>
  <span style="color: #b1b100">if</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
      <span style="color: #b1b100">foreach</span><span style="color: #009900">(</span><span style="color: #000088">$value</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$k</span><span style="color: #339933">=></span><span style="color: #000088">$v</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
        <a rel="noopener noreferrer nofollow" href="http://www.php.net/setcookie" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">setcookie</span></a><span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">.</span><span style="color: #0000ff">'['</span><span style="color: #339933">.</span><span style="color: #000088">$k</span><span style="color: #339933">.</span><span style="color: #0000ff">']'</span><span style="color: #339933">,</span> <span style="color: #000088">$v</span><span style="color: #339933">,</span> <span style="color: #000088">$time</span><span style="color: #339933">,</span> <span style="color: #000088">$path</span><span style="color: #339933">,</span> <span style="color: #000088">$domain</span><span style="color: #339933">,</span> <span style="color: #000088">$s</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
      <span style="color: #009900">}</span>
  <span style="color: #009900">}</span><span style="color: #b1b100">else</span><span style="color: #009900">{</span>
        <a rel="noopener noreferrer nofollow" href="http://www.php.net/setcookie" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">setcookie</span></a><span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #339933">,</span> <span style="color: #000088">$value</span><span style="color: #339933">,</span> <span style="color: #000088">$time</span><span style="color: #339933">,</span> <span style="color: #000088">$path</span><span style="color: #339933">,</span> <span style="color: #000088">$domain</span><span style="color: #339933">,</span> <span style="color: #000088">$s</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
  <span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #666666;font-style: italic">//调用方法</span>
 
cookie<span style="color: #009900">(</span><span style="color: #0000ff">"website"</span><span style="color: #339933">,</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #339933">,</span><span style="color: #0000ff">"./"</span><span style="color: #339933">,</span><span style="color: #0000ff">"www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #666666;font-style: italic">//</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$_COOKIE</span><span style="color: #009900">[</span><span style="color: #0000ff">"website"</span><span style="color: #009900">]</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出WEB开发笔记</span>