
Because the session has already started automatically, session initialization settings can no longer be applied. This means that ini_set(‘session.gc_maxlifetime’, $name[‘expire’]);ini_set(‘session.cookie_lifetime’, $name[‘expire’]); both fail to take effect, so it is basically certain that the session approach cannot be resolved for now.
I had to turn to cookies to implement this. Although this introduces certain security concerns, it necessitates encrypting the cookie to implement the auto-login feature:
// If auto-login is checked, set COOKIE[‘autologin’]
if(I(‘post.autologin’) == 1) {
//$expire = 1209600; // Save for two weeks
$autologin = serialize(array(‘uid’ => $uid, ‘last_login_time’ => time(), ‘last_login_ip’ => get_client_ip(1)));
cookie(‘autologin’, think_encrypt($autologin), 1209600);
}
![If auto-login is checked, set COOKIE['autologin']](https://media.shuijingwanwq.com/2015/05/10.png)
think_encrypt is the encryption function. When the user has logged out of the application but COOKIE[‘autologin’] still exists, the following is required:
/* Auto-login within two weeks */
if(!is_login() && cookie(‘autologin’)) {
$autologin = unserialize(think_decrypt(cookie(‘autologin’)));
if (is_array($autologin) && $autologin[‘last_login_ip’] == get_client_ip(1)) {
/* Log in user */
$Member = D(‘Member’);
$Member->login($autologin[‘uid’]);
}
}
Here, think_encrypt and think_decrypt are the encryption and decryption functions, respectively.
The basic logic is roughly as described above.
