我在Codeigniter
开发了一个应用程序,一切都很顺利,但我有一个问题如下:
首次登录前端和后端后,我想在仪表板上实施autologin
说明:
当我第一次登录admin side
时,会转到admin dashboard
并关闭tab/browser
而不点击logout button
并将admin
链接转到管理信息中心没有再次登录,到目前为止一切进展顺利,但是当我放front end link (after first time login and closing tab)
时它没有进入admin
仪表板并显示错误,如
页面未正确重定向
我在前端遇到同样的问题 我想这样做是因为这是客户要求 请帮我解决这个问题。感谢。
这是我的索引函数
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 :(得分:0)
我认为问题是当浏览器关闭时$ cookie正在消耗, 我建议使用$ _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());
}
答案 1 :(得分: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;
}