0

I want to store the contents of local storage in a PHP variable without having to use a POST. I found this way, but it returns a string whose length is equal to the length of the entire script and the value is equal to the value stored in local storage. It is not used to make subsequent comparisons of the stored value.

Example:

$status = "<script>document.write(localStorage.getItem('click_on_menu'))</script>"; 

(local storage contains "ok")

var_dump($status); -> string(71)"ok"

when you do a comparison

strcmp($status,"ok")

returns that they are not equal (-1) because string length of the variable is 71

If I cast it to an integer and handle it with numbers:

$status =(int) "<script>document.write(localStorage.getItem('click_on_menu'))</script>"; (local storage contains 1)

I have also tried with settype, intval with the same result

It throws me the following

var_dump($status); -> int(0) 

and does not read what is actually stored.

Any solution for this situation? Thanks for your time

AjRoBSeYeR
  • 21
  • 1
  • 1
    `string(71)"ok"` I am not really convinced `ok` string takes 71 characters – Marcin Orlowski May 20 '23 at 18:48
  • 3
    I don't think you can send data from client to server without new request to server. – kennarddh May 20 '23 at 18:48
  • 1
    How can you get result "ok" from the `$status = ""; ` line? $status holds a string which can be outputted in document like `... = $status ?> ...` and this would result in a script being run and write the got local storage item as a salt text where it's called in document. So you cannot get the result of this script into a PHP variable. That's the number 1 mistake. This mistake is probably causing the rest of the process to fail. There're other ways to get local storage data into the PHP. – M. Çağlar TUFAN May 20 '23 at 18:59
  • code result – AjRoBSeYeR May 20 '23 at 19:33
  • 1
    ok then i understand that this procedure is totally wrong. I have seen that they do it by making an ajax request through POST, I will try it there, although the problem is having to call another php file again and refresh the page having changed said value.... anyway, I will do tests On that side, thank you very much everyone! – AjRoBSeYeR May 20 '23 at 19:39

0 Answers0