简体   繁体   English

检查PHP是否存在HTML5 sessionStorage值(与cookie一样)

[英]Check if HTML5 sessionStorage value exists with PHP (like with cookies)

With PHP it's possible to check if a cookie exists. 使用PHP,可以检查cookie是否存在。 Is there a similar way to check if a HTML5 sessionStorage (webstorage) item exists? 是否有类似的方法来检查HTML5 sessionStorage(webstorage)项是否存在?

If you mean by sessionStorage the HTML5 web storage (as I understand from the tags in your question), then no. 如果你的意思是sessionStorage HTML5网络存储(正如我从你问题中的标签那样理解),那么没有。 It is not possible to access this data from PHP. 无法从PHP访问此数据。 It can only be accessed with JavaScript and passed through to PHP with AJAX. 它只能通过JavaScript访问,并通过AJAX传递给PHP。

不是.PHP是服务器端语言,存储或缓存依赖于浏览器,因此无法直接从服务器检查存储或缓存。

This worked. 这很有效。

HTML side: HTML方面:

<body onLoad="submitform()">

<form name="myform" method="post" action="example.php">
   <input type="text" name = "var" id="var">
</form>

<script type="text/javascript">

function submitform()
{
  document.getElementById("var").value = sessionStorage.getItem('var');
  document.myform.submit();
}
</script>

PHP side: PHP方面:

echo $_REQUEST['var'];

You want to pass a variable from HTML5 into PHP, which requires auto-submit. 您想将HTML5中的变量传递给PHP,这需要自动提交。 Try this, though I've never tried it myself. 试试这个,虽然我自己从未尝试过。

Create an invisible text input. 创建一个不可见的文本输入。 Have JavaScript change the input value to the value returned from sessionStorage.getItem("key"), and use the onload='this.form.submit()' line in the tag. 让JavaScript将输入值更改为sessionStorage.getItem(“key”)返回的值,并使用标记中的onload ='this.form.submit()'行。

 <form name = 'phpvar' onload='this.form.submit()'><br>
 <input type = "text" value = sessionStorage.getItem("key")>

I didn't actually do the work for you, but I hope this gives you a good idea. 我实际上并没有为你做这项工作,但我希望这会给你一个好主意。

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

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