Home  >  Article  >  Backend Development  >  获取cookie值解决方法

获取cookie值解决方法

WBOY
WBOYOriginal
2016-06-13 13:32:51788browse

获取cookie值
index.php

HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<iframe src="main.php" style="display:''"></iframe>



main.php
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
setcookie("l_cookie","cookie值");



我现在想要在index.php里获取cookie值,怎么获取不到啊????
我是直接echo $_COOKIE["l_cookie"]的

------解决方案--------------------
别说index.php

就是main.php也没这个值,除非你setcookie后,马上赋值


要么放在index.php里赋值,同时赋值,,,要么借助js取值
------解决方案--------------------
设置cookie,是当前请求完成后才存到客户端的,所以在请求过程中是取不到当前设置的cookie值的(请求 未完成),只能取到上次访问时设置的cookie值。
像楼上说的,刷新下就有了,相当于第2 次访问的时候获取第一次访问时设置的cookie
Statement:
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