0

I have some value inside localtorage. For example, a code like this has set the localStorage :

<script>
    localStorage.setItem('jeton', 'toto');
    localStorage.setItem('cle', 'l1s7T4O7p79XDS9UwfG4YTBhkjoybjHBydC74VxgmXk=')
</script>

Then the file control.php want to access to the local storage :

<?php
    $token2  = JWT::decode(
         $jeton, 
        $cle , // The signing key
        array('HS512') 
    );

The problem is that $jeton et $cle has no value

How to put the localStorage.getItem('jeton') inside $jeton ?

Best regards.

C Morell
  • 81
  • 1
  • 3
  • 10
  • 1
    You need to use some Ajax and https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming – executable Dec 12 '19 at 15:02

3 Answers3

1

The answer to this question is, it not possible to access localStorage at PHP, as localStorage is kept on the client-side only, and there is no data transmitted to the server-side.

Also, you can only get the data with JavaScript and you can send it to the server-side with Ajax only.

The alternative would be to use cookies, if its a simple string, as cookies are sent to the server in the request headers, and accessible at PHP

Sarvesh Mahajan
  • 914
  • 7
  • 16
0

Javascript runs in client-side and PHP runs in server-side. There is no way that they can have access in different environments.

The only way is by using requests. You should send the value from javascript to php using ajax from jQuery

Example using jQuery:

$.ajax({
        url: "/file.php",
        type: "post",
        data: { name: localStorage.getItem('jeton') }
});

Then in file.php you would get that value like this:

$name = $_POST['name'];

You can read more here about ajax.

Edison Biba
  • 4,384
  • 3
  • 17
  • 33
0

So inside control.php :

<script>
//var jeton = localStorage.getItem('jeton');
//var cle = localStorage.getItem('cle');
$.ajax({
    url: "/control.php",
    type: "post",
    data: { jeton: localStorage.getItem('jeton') }
});
</script>

And then inside php:

$jeton = $_POST['jeton'];

But when i execute this I have the error : Undefined index: jeton in C:\inetpub\wwwroot\menergie-client\control.php on line 42.

as if $_POST['jeton'] was not set.

Is it possible to put the javascript and the php in the same file ???

C Morell
  • 81
  • 1
  • 3
  • 10