[英]PHP/JavaScript cookie acting like a pointer so when I clear it, I lose the value; how do I grab the value from the cookie?
<script type="text/javascript">
var test = "test";
document.cookie = "some_test=" + guid;
</script>
<?php $_POST['important_value'] = ((isset($_COOKIE['some_test'])) ? ($_COOKIE['some_test']) : ('')); ?>
<script>
//document.cookie = "some_test=;expires=Thu, 01 Jan 1970 00:00:00 UTC;";
console.log(document.cookie);
</script>
只要我不取消注释清除cookie的行,此代码就可以按预期工作。 目标是将JavaScript变量( test )移到PHP变量( $ _POST ['important_value'] )。
我认为正在发生的事情是$ _POST ['important_value']和$ _COOKIE ['some_test']指向同一件事,但我可能是错的。 反正有打印变量的地址吗?
更新:
debug_zval_dump($_POST['important_value']);
// string(39) "750118664537365903071115537365768136624" refcount(3)
debug_zval_dump($_COOKIE['some_test']);
// string(39) "750118664537365903071115537365768136624" refcount(3)
我假设这意味着我的假设是正确的? 我如何从没有指针的cookie中获取字符串值?
让我们了解您的代码如何工作:
浏览器请求页面yourpage.php。 第一次运行时,cookie为空,因此在服务器端,cookie始终为空。
浏览器使用您的JS加载页面,实际上第一次运行就等于:
<script type="text/javascript"> var test = "test"; document.cookie = "some_test=" + guid; </script> <script> //document.cookie = "some_test=;expires=Thu, 01 Jan 1970 00:00:00 UTC;"; console.log(document.cookie); </script>
浏览器从您的JS接收cookie
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.