[英]Cookies disappear on redirect (PHP)
I created a log-in page and i used cookies for the auto-login option. 我创建了一个登录页面,并使用cookie作为自动登录选项。 For some reason, when i'm trying to test it (going to the log-in page - for testing the redirecting) its not working.
出于某种原因,当我尝试对其进行测试(转到登录页面-用于测试重定向)时,它无法正常工作。 When i'm printing the $_COOKIE i see only the 'PHPSESSID'.
当我打印$ _COOKIE时,我只会看到“ PHPSESSID”。
This is my code: 这是我的代码:
public function index(){
if (isset($_COOKIE[$_SESSION[SESSION_KEY.'id']]) && isset($_COOKIE[$_SESSION[SESSION_KEY.'password']]))
{
$login = $_COOKIE[$_SESSION[SESSION_KEY.'id']];
$password = 1;
}
else if(isset($_POST['login']) && isset($_POST['password']))
{
$password = $_POST['password'];
$login = $_POST['login'];
}
if(isset($login) && isset($password))
{
$query = "SELECT * FROM myDB WHERE id= '{$login}' AND Password = '{$password}'";
$result = $this->db->query($query)->result();
if(count($result) == 0 || count($result) > 1){
$this->load->view('admin/login');
}elseif(count($result) == 1){
$_SESSION[SESSION_KEY.'id'] = $result[0]->id;
$_SESSION[SESSION_KEY.'password'] = 1;
if (isset($_POST['remember']) && isset($_POST['remember']) == 1)
{
setcookie($_SESSION[SESSION_KEY.'id'], $login, time()+60*60*24*10, base_url());
setcookie($_SESSION[SESSION_KEY.'password'], $password, time()+60*60*24*10, base_url());
}
redirect('customers/customers_list');
}
}
else {
$this->load->view('admin/login');
return;
}
}
What could be the problem? 可能是什么问题呢? where are all the cookies?
所有的饼干在哪里? And yes, i have session_start();
是的,我有session_start();
Try to use the php set_cookie() function the first time the user logs in eg 尝试在用户首次登录时使用php set_cookie()函数
setcookie ("username" , $_POST ['username' mktime ()+( 84600 *30 ), "/") Then get the username cookie if it exists, so you can use the stored value anywere you want eg setcookie(“ username”,$ _POST ['username'mktime()+(84600 * 30),“ /”)然后获取用户名cookie(如果存在),因此您可以使用任何需要的存储值,例如
if (isset($_COOKIE ['username' ])) {
//if the cookie exist allow user login e.g
$_SESSION['login']= 'true';
}
else {
//if a cookie doesn't exist
echo "Oops you have to log in!"
//then you display login form
}
Then on the other page you have something like 然后在另一页上,您会看到类似
session_start();
if ($_SESSION['login']='true') {
//Then you display the page
}
else {
//redirect to login page
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.