2013-06-01 128 views
0

我面對與CI中的會話與登錄功能的網站在幾個問題上:笨會話問題

  1. 兩個用戶今天的登錄後,他們將被重定向到主頁的問題報告但他們沒有登錄。如果他們刷新頁面,他們登錄。另外,如果點擊註銷,他們不會被註銷。如果他們之後刷新頁面,則會被註銷。這個問題以前沒有出現過(網站已經運行了近一年),並沒有顯示在我的用戶名/機器上。

  2. 我注意到數據庫中會話表中的重複記錄(相同的IP)。

  3. 一些用戶報告已登錄到另一個人(頂部的消息說,使用他人姓名的歡迎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; 
} 
+0

*「有些用戶報告已登錄另一個人」*這非常糟糕!但是,查看您的配置項設置,會話綁定到IP和UA。因此,對於會話ID,對於多個用戶而言,IP和UA都是相同的,這是不太可能的。你說這個網站已經運行近一年了,這些問題剛剛發生過,就像在PHP或CI更新之後一樣? –

+0

謝謝您的評論!我將IP和UA檢查設置爲TRUE的原因是因爲「用戶登錄到其他用戶」問題。由於我將這些設置設置爲TRUE,所以我沒有幸運地報告過這個問題。最近才發生的問題是1)在我的帖子中。經過一些調試後,我發現問題始終顯示在iPad上,而報告該問題的用戶正在使用Mac。所以我猜這是一個Safari問題。但是我仍然無法解決它。登錄後,沒有會話數據,但在刷新頁面後,會話數據返回。 – PoloRM

+0

但請注意,即使在Mac Safari上也不存在此問題。據我所知,服務器上的PHP版本沒有任何更改或更新,並且我沒有觸及CI文件夾。 – PoloRM

回答

1

真正惡劣的形狀,如果越來越登錄的用戶與其他人的名字。

自動加載會話庫將自動爲用戶代理創建會話,只要有人點擊您的網站。重點是如果有人明確登錄到您的網站,同一個會話應該更新用戶數據。

我想你是自動加載會話庫。將會話cookie名稱的名稱從ci_session更改爲cisession或其他名稱。刪除下劃線,與IE不兼容(美妙的微軟)。它在很多情況下都有幫助。

請分享會話表的架構。確保爲user_agent字段使用varchar(255)。之後,我認爲你不會在同一個IP和用戶代理的數據庫中有多個會話。