检查 PHP 是否存在 HTML5 sessionStorage 值(如 cookie)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16271680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if HTML5 sessionStorage value exists with PHP (like with cookies)
提问by Mark de Vries
With PHP it's possible to check if a cookie exists. Is there a similar way to check if a HTML5 sessionStorage (webstorage) item exists?
使用 PHP 可以检查 cookie 是否存在。是否有类似的方法来检查 HTML5 sessionStorage (webstorage) 项目是否存在?
回答by Lasse
If you mean by sessionStorage the HTML5 web storage (as I understand from the tags in your question), then no. It is not possible to access this data from PHP. It can only be accessed with JavaScript and passed through to PHP with AJAX.
如果您的意思是 sessionStorage 是 HTML5 网络存储(我从您问题中的标签中了解到),那么不。无法从 PHP 访问此数据。它只能通过 JavaScript 访问,并通过 AJAX 传递给 PHP。
回答by az pakistan
No. PHP is server side language and storage or cache are browser dependent, so it is not possible check storage or cache directly from server.
不。PHP 是服务器端语言,存储或缓存依赖于浏览器,因此无法直接从服务器检查存储或缓存。
回答by afukada
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'];
回答by Yellow
You want to pass a variable from HTML5 into PHP, which requires auto-submit. Try this, though I've never tried it myself.
您想将一个变量从 HTML5 传递到 PHP,这需要自动提交。试试这个,虽然我自己从未尝试过。
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.
我实际上并没有为你做这项工作,但我希望这能给你一个好主意。