Callum Holden
Callum Holden

Reputation: 177

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.

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:

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

PHP:

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

Upvotes: 0

Views: 37

Answers (1)

adeneo
adeneo

Reputation: 318182

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.
sessionStorage is a clientside storage in the browser only, it can't be accessed from the serverside.

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.

Upvotes: 1

Related Questions