简体   繁体   中英

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

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?

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.

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

This worked.

HTML side:

<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:

echo $_REQUEST['var'];

You want to pass a variable from HTML5 into PHP, which requires auto-submit. 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.

 <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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