2015-04-28 40 views
0

Codeigniter一切開發的一個應用是完全準備,但我有一個問題如下:
我想要實現autologin兩個前端和背部首次登錄後到儀表板結束。
說明:
當我第一次去admin dashboard和關閉tab/browser沒有點擊logout button登錄admin side,並再次把admin鏈接它去管理儀表板沒有登錄,高達這裏一切都很順利,但當我把front end link (after first time login and closing tab)它不走的admin儀表盤和節目,如笨的Cookie問題的後端和前端側

錯誤的頁面不重定向正確

我在前端面臨同樣的問題。
我想這樣做,因爲它是客戶的要求。
請幫我解決這個問題。謝謝。

這裏是我的索引功能

if($_COOKIE['AS_AD_SES_ADMIN_ID']!='' && $_COOKIE['stay_signedin']=='1') 
{ 
     $username = $_COOKIE['ADMIN_USERNAME']; 
     $password = $_COOKIE['ADMIN_PASS']; 

     $result = $this->verifylogin->login($username, $password); 

     foreach($result as $row) 
     { 
      $sess_array = array(
        'AS_AD_SES_ADMIN_ID'   => $row->fld_id, 
        'AS_AD_SES_ADMIN_FULL_NAME' => $row->fld_user_name, 
        'AS_AD_SES_ADMIN_NAME'   => $row->fld_adm_name, 
        'AS_AD_SES_LOGIN_TIME'   => date('h:i:s A'), 
        'AS_AD_SES_LOGIN_DATE'   => date("d-m-Y"), 
        'AS_SES_ACCESS_LEVEL'   => $row->fld_access_level, 
        'AS_TAB_TITLE'     => 'Welcome to Project', 
        'logged_in'     => TRUE 
        ); 

      $this->session->set_userdata($sess_array); 
     } 
     $this->load->view('dashboard'); 
}else{ 
     redirect(base_url()); 
} 

Verifylogin車型登錄功能如下

function login($username, $password) 
{ 
    $ip_address=$_SERVER['REMOTE_ADDR']; 

    $this -> db -> select('fld_id, fld_user_name,fld_adm_name,fld_adm_pwd,fld_access_level'); 
    $this -> db -> from('tbl_admin'); 
    $this -> db -> where('fld_user_name',$username); 
    $query = $this -> db -> get(); 

    if($query -> num_rows() == 1) 
    { 
     $res = $query->row(); 
     $pass = $res->fld_adm_pwd; 
     $pass1 = $this->encrypt->decode($pass); 
     if($pass1 == $password) 
     { 
      $password = $pass; 

      $query = $this->db->query("SELECT fld_id,fld_user_name,fld_adm_name,fld_access_level,fld_adm_pwd,fld_stay_signed_in FROM tbl_admin where fld_user_name = '".$username."' and fld_adm_pwd = '".trim($password)."' and fld_isdeleted !=1 and fld_status = 'Active'"); 

      if($query->num_rows() == 1) 
      { 
       return $query->result(); 
      } 
     } 
     else 
     { 
      return false; 
     } 
    } 

} 
+0

你的代碼在哪裏? – Saty

+0

@saty感謝您的即時回覆,我在管理員登錄時使用了cookie,然後爲它設置了cookie。 –

+0

我們不能幫助你,直到我們無法看到你的代碼? – Saty

回答

0

我認爲問題是,當瀏覽器關閉時$ cookie被dieing, 我建議使用$ _SESSION變量。

// Start the session 
session_start(); 

if($_SESSION['AS_AD_SES_ADMIN_ID']!='' && $_SESSION['stay_signedin']=='1') 
     { 
      $username = $_SESSION['ADMIN_USERNAME']; 
      $password = $_SESSION['ADMIN_PASS']; 

      $result = $this->verifylogin->login($username, $password); 

      foreach($result as $row) 
      { 
       $sess_array = array(
         'AS_AD_SES_ADMIN_ID'   => $row->fld_id, 
         'AS_AD_SES_ADMIN_FULL_NAME' => $row->fld_user_name, 
         'AS_AD_SES_ADMIN_NAME'   => $row->fld_adm_name, 
         'AS_AD_SES_LOGIN_TIME'   => date('h:i:s A'), 
         'AS_AD_SES_LOGIN_DATE'   => date("d-m-Y"), 
         'AS_SES_ACCESS_LEVEL'   => $row->fld_access_level, 
         'AS_TAB_TITLE'     => 'Welcome to Project', 
         'logged_in'     => TRUE 
         ); 

       $this->session->set_userdata($sess_array); 
      } 
      $this->load->view('dashboard'); 
     }else{ 
      redirect(base_url()); 
     } 
+0

我想實現像在Codeigniter中記住我的功能,爲此,如果用戶在登錄表單上檢查記住我複選框,我必須使用&設置cookie 2周。 –

+0

set_userdata用於會話變量,在您的情況下爲cookie,您需要設置cookie – LoneRanger

+0

http://www.w3schools.com/php/func_http_setcookie.asp,鑑於如何設置cookie。 – LoneRanger

0

如果您verifylogin->login函數將返回false,那麼你也加載儀表板視圖。你需要檢查它是否返回錯誤或結果。而且你不需要爲單一結果而使用foreach。

if($_COOKIE['AS_AD_SES_ADMIN_ID']!='' && $_COOKIE['stay_signedin']=='1') 
{ 
    $username = $_COOKIE['ADMIN_USERNAME']; 
    $password = $_COOKIE['ADMIN_PASS']; 

    $row = $this->verifylogin->login($username, $password); 

    if($row) 
    { 
     $sess_array = array(
       'AS_AD_SES_ADMIN_ID'   => $row->fld_id, 
       'AS_AD_SES_ADMIN_FULL_NAME' => $row->fld_user_name, 
       'AS_AD_SES_ADMIN_NAME'   => $row->fld_adm_name, 
       'AS_AD_SES_LOGIN_TIME'   => date('h:i:s A'), 
       'AS_AD_SES_LOGIN_DATE'   => date("d-m-Y"), 
       'AS_SES_ACCESS_LEVEL'   => $row->fld_access_level, 
       'AS_TAB_TITLE'     => 'Welcome to Project', 
       'logged_in'     => TRUE 
       ); 

     $this->session->set_userdata($sess_array); 
     $this->load->view('dashboard'); 
    }else{ 
     redirect(base_url()); 
    } 
}else{ 
    redirect(base_url()); 
} 


function login($username, $password) 
{ 
    $ip_address=$_SERVER['REMOTE_ADDR']; 

    $this -> db->select('fld_id, fld_user_name,fld_adm_name,fld_adm_pwd,fld_access_level'); 
    $this -> db -> from('tbl_admin'); 
    $this -> db -> where('fld_user_name',$username); 
    $query = $this -> db -> get(); 

    if($query -> num_rows() == 1) 
    { 
     $res = $query->row(); 
     $pass = $res->fld_adm_pwd; 
     $pass1 = $this->encrypt->decode($pass); 
     if($pass1 == $password) 
     { 
      $password = $pass; 

      $query = $this->db->query("SELECT fld_id,fld_user_name,fld_adm_name,fld_access_level,fld_adm_pwd,fld_stay_signed_in FROM tbl_admin where fld_user_name = '".$username."' and fld_adm_pwd = '".trim($password)."' and fld_isdeleted !=1 and fld_status = 'Active'"); 

      if($query->num_rows() == 1) 
      { 
       return $query->row(); 
      } 
     } 
    } 
    return false; 
}