我在具有登录功能的网站上面临与 CodeIgniter 中的会话有关的几个问题:
今天有两个用户反映了一个问题,登录后跳转到首页但是没有登录,刷新页面就是登录了登出。如果他们之后刷新页面,他们将被注销。这个问题以前没有出现过(网站已经运行了将近一年),也没有出现在我的用户名/机器上。
我注意到数据库的会话表中有重复的记录(相同的 IP)。
一些用户报告说正在登录另一个人(顶部的消息说欢迎 X 使用另一个人的名字)。我已经对代码进行了四次检查,以查找可能实际上使用户登录到另一个用户名的任何问题,但找不到任何漏洞。
任何解决这些问题的帮助和提示将不胜感激。这是我的会话设置:
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
到目前为止,我发现这些设置效果最好。下面是登录控制器功能:
public function index()
{
// if user is already logged in, redirect him to main
if ($this->users_model->check_if_logged_in())
redirect(site_url('gmat/' . format_gmat_url()));
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_message('required', 'The credentials you entered are invalid.');
$this->form_validation->set_rules('signin_email', 'Email', 'trim|required|max_length[100]|xss_clean');
$this->form_validation->set_rules('signin_pswd', 'Password', 'trim|required|max_length[25]|md5|xss_clean');
$this->form_validation->set_rules('signin_auth', 'Authorization Code', 'trim|required|max_length[100]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('error_msg', 'Invalid login, please try again.');
redirect(site_url('gmat/' . format_gmat_url() . 'sign_up'));
}
else
{
// Check if user is valid
$u_email = $this->input->post('signin_email');
$u_password = $this->input->post('signin_pswd');
$u_auth = (format_gmat_url() == 'lev2' && $this->input->post('signin_auth') == 0 ? 'notvalid' : $this->input->post('signin_auth')); // for level 2 sign in
$login_result = $this->users_model->check_login($u_email, $u_password, 0, $u_auth);
if ($login_result['success'] == TRUE)
{
redirect(site_url('gmat/' . format_gmat_url()));
}
else
{
$this->session->set_flashdata('error_msg', $login_result['error_msg']);
redirect(site_url('gmat/' . format_gmat_url() . 'sign_up'));
}
}
}
$u_auth 和与“2 级登录”相关的任何内容都允许登录用户访问另一个“子站点”。主站点的登录页面不使用与其相关的任何内容或使用验证码登录。
模型函数 check_login:
public function check_login($email, $password, $admin = 0, $auth_code = 0)
{
$login_result = array('success' => FALSE, 'error_msg' => '');
$this->load->model('users_model');
$user_condition = "u_email = '" . $email . "' AND u_password = '" . $password . "' AND u_authorized = '' AND u_active = 1";
$user_condition .= ($admin == 1 ? " AND u_admin = 1" : "");
$user_condition .= ($auth_code != 0 ? " AND u_access_level2 = 1" : "");
$user = $this->users_model->get_db_users($user_condition);
$u_logged_in_level2 = FALSE;
if(count($user) == 1)
{
if ($auth_code != 0 || $auth_code != '0')
{
$this->load->model('settings_model');
// Signing in on level 2, check for authorization code
$settings = $this->settings_model->get_db_settings("s_code = 'level_2_authorization'");
if ($settings[0]['s_value'] != $auth_code)
{
$login_result['success'] = FALSE;
$login_result['error_msg'] = 'Invalid authorization code.';
return $login_result;
}
else
{
$u_logged_in_level2 = TRUE;
}
}
$login_result['success'] = TRUE;
$this->session->sess_destroy();
$this->session->sess_create();
// Create session data
$data = array(
'user_id' => $user[0]['u_id'],
'user_email' => $email,
'user_first_name' => $user[0]['u_first_name'],
'user_family_name' => $user[0]['u_family_name'],
'user_father_name' => $user[0]['u_father_name'],
'user_mobile' => $user[0]['u_mobile'],
'user_admin' => $user[0]['u_admin'],
'access_level2' => $u_logged_in_level2,
'logged_in' => TRUE
);
$this->session->set_userdata($data);
}
else
{
$login_result['error_msg'] = 'Invalid login, please try again.';
}
return $login_result;
}