【问题标题】:php cookie change to NULL when jump to another page跳转到另一个页面时php cookie更改为NULL
【发布时间】:2020-08-12 11:24:47
【问题描述】:

在我的控制器/Welcome.php 中,我包含了一个登录功能。 Rhis 不是完整的代码。我只包含代码的关键部分,以便您可以更清楚地阅读它。我将默认login_status设置为false。当用户有正确的用户名和密码时,login_status被设置为true。 但是,在我的项目/Home.php 中,我尝试检索 cookie“login_status”。但是 cookie 变成了NULL。 代码关键部分如下:

controller/Welcome.php 公共函数 login()


        $this->input->set_cookie('login_status',FALSE,time()+3600);//delete or not

        if($this->input->cookie('login_status')==FALSE){    

            //The following code means login password match the username
            elseif($infomatch){

                if($remember == 1){
                    $this->input->set_cookie('username',$username,time()+3600);
                    $this->input->set_cookie('password',$password,time()+3600);
                    $this->input->set_cookie('remember',$remember,time()+3600);
                }
                else
                {
                    $this->input->set_cookie('username',"",time()-3600);
                    $this->input->set_cookie('password',"",time()-3600);
                    $this->input->set_cookie('remember',"",time()-3600);
                }
                $this->input->set_cookie('login_status',TRUE,time()+3600);//hihihihihi
                redirect('home');

            }

    }

控制器/Home.php

if($this->input->cookie('login_status')==FALSE){
            header("location: Welcome/login");
        } else{


            $this->session->set_userdata('login_status',TRUE);

            $this->load->database();
            $this->load->model('scheduler_model');
            $this->load->view('header.php');

            $data['username'] = $this->session->userdata('username');

            $data["getnames"]=$this->scheduler_model->get_users();
            $this->load->view('home/homepage',$data);

            $data["getassignments"]=$this->scheduler_model->get_assignment();
            $data["getselectedcourses"]=$this->scheduler_model->get_select_course();
            $this->load->view('home/assignments',$data);  
        }

【问题讨论】:

  • 永远不要在 cookie 中存储密码。使用服务器端会话。

标签: php codeigniter authentication cookies


【解决方案1】:

Cookie 是纯文本。您不能在 Cookie 中存储布尔值。您可以做的是存储一个表示布尔值的字符串。例如:1 for true0 for false

$this->input->set_cookie('login_status','0',3600); //set it to '1' in the log in process
//also notice: expiration is how long it should remain in seconds

检查

if($this->input->cookie('login_status')== '0'){

【讨论】:

  • 您好,感谢您的回答。但是,问题仍然存在,当我进入个人资料页面时,login_status cookie 变为 NULL。
  • 更新:删除time()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 2011-03-02
  • 2021-03-11
  • 1970-01-01
  • 2020-10-27
  • 2019-10-25
相关资源
最近更新 更多