将JS LocalStorage加载到PHP变量中 [英] Loading JS LocalStorage into PHP Variable

查看:444
本文介绍了将JS LocalStorage加载到PHP变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的文档标题中,我正在定义电子邮件和密码.我想用户本地存储.但是,我无法从localstorage将项目加载到我的php vars中.

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

这是我的代码.

<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>

我知道我正在设置本地存储权限,因为我正在检查chrome,并且key和value都是正确的.我只是很难将键的值传递到我的php的define()中.

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 have trouble passing the value of the keys into the define() of my php.

感谢您的帮助!

推荐答案

您无法通过PHP访问localstorage.您需要编写一些将本地存储数据发送回脚本的JavaScript.

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

如果您使用的是jQuery,请执行以下操作.

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.
?>

请记住,人们可以编辑其本地存储中的内容,因此永远不要相信用户的输入.狡猾的入侵者也可以简单地开始将数据发布到您的login_response.php页面.

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.

这篇关于将JS LocalStorage加载到PHP变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆