将 JS LocalStorage 加载到 PHP 变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10923813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading JS LocalStorage into PHP Variable
提问by Chris Olson
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.
在我的文档的标题中,我定义了一个电子邮件和密码。我想使用本地存储。但是,我无法将本地存储中的项目加载到我的 php 变量中。
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 have trouble passing the value of the keys into the define() of my php.
我知道我正在正确设置 localstorage 因为我正在检查 chrome 并且键和值都是正确的。我只是在将键的值传递到我的 php.ini 的 define() 时遇到了麻烦。
Thanks for the help!
谢谢您的帮助!
回答by GameCharmer
You cannot access localstorage via PHP. You need to write some javascript that sends the localstorage data back to the script.
您无法通过 PHP 访问 localstorage。您需要编写一些 javascript 将本地存储数据发送回脚本。
If you are using jQuery, do something like the following.
如果您使用 jQuery,请执行以下操作。
set_page.php
set_page.php
<script>
localStorage.setItem('email', '<?php echo $_SESSION['email'];?>');
localStorage.setItem('password', '<?php echo $_SESSION['password'];?>');
</script>
login_page.php
登录页面.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
登录响应.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.
请记住,人们可以编辑本地存储中的内容,因此永远不要相信用户输入。狡猾的入侵者也可以简单地开始将数据发布到您的 login_response.php 页面。
回答by Sam
This is errate, do so
这是错误的,这样做
<script>
localStorage.setItem('email', "<?php echo $_SESSION['email'];?>");
localStorage.setItem('password', "<?php echo $_SESSION['password'];?>");
</script>
回答by Jo Smo
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.
如果您只需要保存电子邮件和密码(小数据),那么您可以为此使用 cookie,您可以通过 PHP 和 JavaScript 访问这些 cookie。
How to set a cookie in PHP:
如何在 PHP 中设置 cookie:
setcookie("TestCookie", $value, time()+3600, "/", "example.com");
How to delete a cookie:
如何删除cookie:
setcookie("TestCookie", $value, time()-3600, "/", "example.com");
How to get a cookie in PHP:
如何在 PHP 中获取 cookie:
$_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"]).
表示 cookie 只能通过安全的 HTTPS 连接从客户端传输。设置为 TRUE 时,仅当存在安全连接时才会设置 cookie。在服务器端,程序员只能在安全连接上发送这种 cookie(例如,关于 $_SERVER["HTTPS"])。
Source: https://secure.php.net/manual/en/function.setcookie.php

