How to create “remember me checkbox” using Codeigniter session library?

后端 未结 7 1170
梦如初夏
梦如初夏 2020-12-12 18:30

in Codeigniter I am building an Authentication system for my web site and to achieve that I use session library

     session->set_userdata(\'username\')
<         


        
7条回答
  •  甜味超标
    2020-12-12 19:04

    I needed the same thing. You cannot accomplish this using CI settings so I have chosen to override the setcookie method of the CI Session class (in MY_Session):

    function _set_cookie($cookie_data = NULL)
    {
        if (is_null($cookie_data))
        {
            $cookie_data = $this->userdata;
        }
    
        // Serialize the userdata for the cookie
        $cookie_data = $this->_serialize($cookie_data);
    
        if ($this->sess_encrypt_cookie == TRUE)
        {
            $cookie_data = $this->CI->encrypt->encode($cookie_data);
        }
        else
        {
            // if encryption is not used, we provide an md5 hash to prevent userside tampering
            $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
        }
    
        setcookie(
                    $this->sess_cookie_name,
                    $cookie_data,
                    $this->userdata('rememberme') == true ? $this->sess_expiration + time() : 0,
                    $this->cookie_path,
                    $this->cookie_domain,
                    0
                );
    
    }
    

    Of course you need to set the rememberme flag in your session based upon the choice the user made.

提交回复
热议问题