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

4

6

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?

Shrapnel answered 29/4, 2013 at 5:23 Comment(0)
H
7

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.

Holm answered 29/4, 2013 at 5:32 Comment(1)
I indeed meant the HTML5 web storage. Thanks for your answer.Shrapnel
S
1

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.

Starch answered 1/11, 2015 at 10:54 Comment(2)
You mean "cookie"?Known
I think he meant exactly what he said. PHP can access cookies.Gamely
S
1

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'];
Spermatophore answered 28/5, 2016 at 13:8 Comment(1)
when echo $_REQUEST['var']; Undefined index: varRobbirobbia
Z
0

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.

Zacharyzacherie answered 17/7, 2015 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.