Cookie不会设置为保持我登录。PHP [英] Cookie will not set for keep me logged in. PHP

查看:156
本文介绍了Cookie不会设置为保持我登录。PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试添加保持我登录。不确定我做错了什么,但在初始登录时,保持我登录被选中,这将运行:

Attempting to add a "keep me logged in". Not exactly sure what I'm doing wrong, but on the initial login when "keep me logged in" is checked, this will run:

$email_and_password = array($email_from_html, $pass_from_html);
setcookie("StayLoggedZod", $email_and_password, time()+3600*24*30);

$ email_from_html& $ pass_from_html是用户的电子邮件和密码存储在数组中,并存储到cookie中。

$email_from_html & $pass_from_html are the user's email and password being stored into an array and that being stored into a cookie.

现在当您离开后返回网站时,这样做:

Now when you go back to the site after leaving, the initial page does this:

if (isset($_COOKIE['StayLoggedZod'])) 
{
$email_and_password = $_COOKIE['StayLoggedZod'];
$stored_email = $email_and_password[0];
$stored_password = $email_and_password[1];
//USES THE E-MAIL AND PASS STORED IN HERE TO LOG INTO WEBSITE VIA MySQL DB, I EXCLUDED THIS CODE BECAUSE I KNOW THIS PART WORKS.
}
else
{
header("location:login.php");
}

它不返回错误,我无法使其工作,这样的快速测试页:

It returns no errors, I couldn't get it to work so I made a quick test page like this:

$cookie_data = $_COOKIE['StayLoggedZod'];
$the_email = $cookie_data[0];
$the_pass = $cookie_data[1];
$display_string = "This is my email".$the_email.". This is my password".$the_pass;
echo $display_string;

除了这是我的电子邮件等外没有数据。

No data shows up aside from "This is my email" ect.

这是我第一次做一个保持我的登录,不知道我错过了什么,希望我彻底与我的解释。
-Mike

This is my first time making a keep my logged in, not sure what I'm missing, hope I was thorough with my explanation. -Mike

推荐答案

1)您不应将密码等敏感信息存储在Cookie中。 Cookie存储在客户端,可编辑和可见

1) you should not store sensitive details like the password in a cookie. Cookies are stored on the client side and are editable and visible

2)检查setcookie的手册页 http://php.net/setcookie

2) check the man page for setcookie http://php.net/setcookie

您将看到第二个参数(要存储的值)应该是一个字符串(所以如果你想存储数组想一个方法将它们存储为一个字符串,如序列化或json编码)

you will see that the second parameter (the value that you want to store) should be a string (so if you want to store arrays think of a method to store them as a string like serialization or json encoding)

这篇关于Cookie不会设置为保持我登录。PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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