登录部分在php codeigniter中工作不正常
我正在为我的项目使用php codeigniter。在我的登录页面中,如果用户名和密码无效,只需加载登录页面,否则加载主页。如果无效,第一次再次加载登录页面时,如果登录详细信息错误,则会在url中添加一个控制器名称,如localhost/project name/administrator/administrator/login\u authentication 我的代码是登录部分在php codeigniter中工作不正常,php,codeigniter,Php,Codeigniter,我正在为我的项目使用php codeigniter。在我的登录页面中,如果用户名和密码无效,只需加载登录页面,否则加载主页。如果无效,第一次再次加载登录页面时,如果登录详细信息错误,则会在url中添加一个控制器名称,如localhost/project name/administrator/administrator/login\u authentication 我的代码是 function index() { if($this->session->userdata('usert
function index()
{
if($this->session->userdata('usertype') != '')
{
redirect('administrator/administrator_view');
}
else
{
$this->load->view('login');
}
}
function login_authentication()
{
$username=$this->input->post('username');
$password=$this->input->post('password');
$user = $this->administrator->admin_authentication($username,$password);
if(count($user) == 1)
{
foreach($user as $admin_value)
{
$user_name=$admin_value['UserName'];
$usertype=$admin_value['UserType'];
}
$session_data = array(
'username' => $user_name,
'usertype' => $usertype,
);
$this->session->set_userdata($session_data);
if($usertype == 1)
{
redirect('administrator/administrator_view');
}
}
else
{
$data['Invalid_Login']="Invalid Username and Password";
$this->load->view('login',$data);
}
}
function administrator_view()
{
if($this->session->userdata('usertype') == '')
{
redirect('administrator');
}
else
{
$data['heading'] = '';
$this->load->view('header', $data);
$this->load->view('dashboard', $data);
$this->load->view('footer');
}
}
管理员身份验证功能
function admin_authentication($username, $password)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('UserName',$username);
$this->db->where('Password',$password);
$query = $this->db->get();
return $query->result_array();
}
每次在url中添加一个控制器名称时,我都会尝试多次提供不正确的登录信息。请帮帮我
提前感谢。首先检查您的函数登录\u身份验证中是否有post请求,如下所示:
function login_authentication()
{
if( $this->input->post(null) ){
//your authentication code here
}else{
//load the login view here
}
}
以下是您的功能:
function login_authentication(){
if( $this->input->post(null) ){ //check if there is an post request
$username=$this->input->post('username');
$password=$this->input->post('password');
$user = $this->administrator->admin_authentication($username,$password);
print_r( $user );die(); //the user array as returned from the model see if its correct or not
if(count($user) == 1)
{
foreach($user as $admin_value)
{
$user_name=$admin_value['UserName'];
$usertype=$admin_value['UserType'];
}
$session_data = array(
'username' => $user_name,
'usertype' => $usertype,
);
print_r( $session_data );die; //see if it builds the correct array or not
//$this->session->set_userdata($session_data);
$this->session->set_userdata('user_info',$session_data); //to read the username use like $this->session->userdata['user_info']['username'];
if($usertype == 1)
{
redirect('administrator/administrator_view');
}
}else{ //invalid credentials load the login view
$this->session->set_flashdata('Invalid_Login', 'Invalid username or password!'); //to echo in view use $this->session->flashdata('Invalid_Login');
redirect('administrator', 'refresh');
}
}else{ //redirect to index function now
redirect('administrator', 'refresh');
}
}
在“功能管理员”视图中
改变
到
改变
if($this->session->userdata('usertype') == '')
在所有领域
$ses = $this->session->userdata('some_name');
if($ses['usertype'] == '')
然后试试……请将控制器代码粘贴到这里。与其尝试重新发明控制盘,不如使用现有的东西让您领先一步。看看[Bonfire][1],它已经有了用户注册、身份验证和基于角色的访问控制,所以您可以专注于应用程序的真正功能。[1] :你能发布你的管理员身份验证功能吗?管理员身份验证功能已更新。问题仍在继续。在你的问题中发布你的模型功能。您将会话设置错误。如下设置:$this->session->Set_userdata'user_info',$session_data;。在您的项目\u root/index.php中,将环境设置为“开发”,这样您也可以调试所有错误。如果提供了正确的用户名和密码,它不会再次转到主页,而是重定向到登录页。您是否更改了管理员视图也更改了管理员视图部分
$this->session->set_userdata(('some_name', $session_data);
if($this->session->userdata('usertype') == '')
$ses = $this->session->userdata('some_name');
if($ses['usertype'] == '')