14

In the header of my doc I am defining an email and password. I would like to use local storage. However I am having trouble loading items from localstorage into my php vars.

Here is my code.

<script>
    localStorage.setItem('email', '<?php echo $_SESSION['email'];?>');  
    localStorage.setItem('password', '<?php echo $_SESSION['password'];?>');
</script>

<?php
    $user_email = "<script>document.write(localStorage.getItem('email'));</script>";
    $password = "<script>document.write(localStorage.getItem('password'));</script>";

    define('XOAUTH_USERNAME', $user_email);
    define('XOAUTH_PASSWORD', $password);
?>

<html>
    <head></head>
    <body></body>
</html>

I know that I am setting the localstorage right because I'm checking in chrome and both key and value are correct. I'm just having problems passing the value of the keys into the define() of my php.

Thanks for the help!

3
  • XMLHttpRequest. Or perhaps <form> submit. But, an HTTP request is the only way to pass data from the client (JavaScript) to the server (PHP). Commented Jun 6, 2012 at 23:52
  • I think there's a mixing between client code and server code here as most of PHP programmers used to do. Why not splitting between server (PHP) and client (JavaScript) code? Commented Jul 9, 2022 at 2:59
  • Why would you want to read them from local storage (client side) where you already have these in your session (server side) Commented Nov 25, 2024 at 7:31

5 Answers 5

16

You cannot access localstorage via PHP. You need to write some javascript that sends the localstorage data back to the script.

If you are using jQuery, do something like the following.

set_page.php

<script>
localStorage.setItem('email', '<?php echo $_SESSION['email'];?>');  
localStorage.setItem('password', '<?php echo $_SESSION['password'];?>');
</script>

login_page.php

<script>
var email = localStorage.getItem('email'), password = localStorage.getItem('password');
$.POST('login_response.php', {'email':email,'password':password}, function(data){
  alert('Login Successful.  Redirect to a different page or something here.');
}
</script>

login_response.php

<?
$email = $_POST['email'];
$password = $_POST['password'];
//Handle your login here.  Set session data, etc.  Be sure to sanitize your inputs.
?>

Remember, people can edit what is in their local storage, so never ever trust user input. A crafty intruder could simply start posting data to your login_response.php page as well.

Sign up to request clarification or add additional context in comments.

Comments

8

If you just need to save an email and password (small data), then you could use cookies for this, which you can access via PHP and via JavaScript.

How to set a cookie in PHP:

setcookie("TestCookie", $value, time()+3600, "/", "example.com");

How to delete a cookie:

setcookie("TestCookie", $value, time()-3600, "/", "example.com");

How to get a cookie in PHP:

$_COOKIE["TestCookie"]

The last parameter in this example:

setcookie("TestCookie", $value, time()+3600, "/", "example.com", 1);

indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. On the server-side, it's on the programmer to send this kind of cookie only on secure connection (e.g. with respect to $_SERVER["HTTPS"]).

Source: https://secure.php.net/manual/en/function.setcookie.php

3 Comments

The question is about LocalStorage. Why are you talking about cookies?
@Maf I came here from a google search for php LocalStorage and this answer made me realise its cookies i should be using.
I also think there's a mixing between client code and server code here as most of PHP programmers used to do.
0

This is errate, do so

<script>
   localStorage.setItem('email', "<?php echo $_SESSION['email'];?>");
   localStorage.setItem('password', "<?php echo $_SESSION['password'];?>");
</script>

6 Comments

Thanks but this doesn't fix the issue.
You can't get the output of client localstorage var
define('XOAUTH_USERNAME', $_SESSION['email']); define('XOAUTH_PASSWORD', $_SESSION['password']);
I would rather it come from the localstorage because that lasts longer.
You can do ini_set('gc_maxlifetime','3600') for increase the session time
|
0

You can access localstorage via PHP. You need to write some javascript that store localstorage value in COOKIE and you can use it using PHP.

<script>
var email = localStorage.getItem("email");
var d = new Date();
d.setTime(d.getTime() + (1*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = 'email' + "=" + email+ ";" + expires + ";path=/";
</script>
<?php 
print_r($_COOKIE['email']);
?> 

Comments

0

I'm sorry to say that if you need to transfer the data from a Local Storage to Cookies it beats the purpose of using Local Storage in the first place. You use Local Storage to avoid using Cookies to optimize user experience. And I reiterate, if that's the purpose of using local storage.

Disadvantage of Cookies:

  1. The storage limit of cookies in web browsers is limited to about 4KB.
  2. Cookies are sent with every HTTP request, thereby slowing down the web application performance.

HTML5 Storage - Sitepoint

The best option is to set up your process allowing PHP to handle the Session then from PHP to Local Storage example (using your own script):

<?php
    $user_email = $_SESSION["user_email"];
    $password = $_SESSION["user_password"];

    define('XOAUTH_USERNAME', $user_email);
    define('XOAUTH_PASSWORD', $password);
?>
<script>
    localStorage.setItem('email', '<?=XOAUTH_USERNAME?>');  
    localStorage.setItem('password', '<?=XOAUTH_PASSWORD?>');
</script>

<html>
    <head></head>
    <body></body>
</html>

You will never be able to access user interaction after the page loads, unless you create a JS script to execute an AJAX login as suggested by @GameCharmer. And of course you need to implement good security practices to avoid Cross Scripting (XSS) attacks from user's input.

P.S. You don't need to use the following statement:

<?php echo $user_password; ?>

Even if shorthand setting is set to off or false in php.ini file you can use:

<?=$user_password?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.