简体   繁体   English

PHP无法访问通过JavaScript代码设置的会话变量

[英]PHP not accessing session variable set via javascript code

I am trying to write some php code which will access a session variable created in javascript, and display a simple alert to the screen with its contents. 我正在尝试编写一些php代码,这些代码将访问用javascript创建的会话变量,并向屏幕上显示一个简单的警报及其内容。

However, the alert is just blank like it is not actually accessing the value of the variable. 但是,警报只是空白,就像它实际上没有在访问变量的值一样。

Could someone please point me in the right direction? 有人能指出我正确的方向吗?

Thanks. 谢谢。

Javascript: 使用Javascript:

var loadCounter = 0;
sessionStorage.setItem("LoadCounter", loadCounter);

PHP: PHP:

$g = $_SESSION['LoadCounter'];
    echo '<script type="text/javascript">alert("'.$g.'");</script>';

Those aren't even remotely the same thing. 这些甚至不是同一回事。

The PHP session is stored on the server, with a key that is stored in a cookie. PHP会话存储在服务器上,密钥存储在cookie中。
sessionStorage is a clientside storage in the browser only, it can't be accessed from the serverside. sessionStorage仅是浏览器中的客户端存储,不能从服务器端进行访问。

If you want to update the PHP session and make the data available on the serverside, you have to send it with ajax and update the PHP session in PHP. 如果要更新PHP会话并使数据在服务器端可用,则必须使用ajax发送数据并在PHP中更新PHP会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM