CodeIgniter登录会话

时间:2016-06-07 00:11:54

标签: php codeigniter session login

我是CodeIgniter的新手,我试图在我的网站上创建一个登录系统。当我尝试进入页面概述时,它会在登录页面上发送给我,但在我输入正确的电子邮件和密码之后没有任何反应。

Login.php - controller

    <?php if ( ! defined('BASEPATH')) exit('No direct script access
allowed');
     class Login extends CI_Controller {
      function __construct()
      {
        parent::__construct();
      }

      function index()
      {
        $this->load->helper(array('form'));
        $this->load->view('login');
      }

     }
    ?>

Overview.php - controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Overview extends CI_Controller {

 function __construct()
 {
   parent::__construct();
 }

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['username'] = $session_data['username'];
     $this->load->view('overview', $data);
   }
   else
   {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
 }

 function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }

}

?>

VerifyLogin.php - controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user','',TRUE);
 }

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('email', 'E-mail', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('login');
   }
   else
   {
     //Go to private area
     redirect('home', 'refresh');
   }

 }

 function check_database($password)
 {
   //Field validation succeeded.  Validate against database
   $username = $this->input->post('username');

   //query the database
   $result = $this->user->login($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}
?>

Login.php - 观看次数

<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4 well">
<form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform">
<fieldset>
<legend>Login</legend>
<div class="form-group">
<label for="name">Email</label>
<input type="text" name="email" placeholder="Your Email" required class="form-control" />
</div>

<div class="form-group">
<label for="name">Password</label>
<input type="password" name="password" placeholder="Your Password" required class="form-control" />       
</div>

<div class="form-group">
<input type="submit" name="login" value="Login" class="btn btn-primary" />
</div>
</fieldset>
</form>
<span class="text-danger"><?php if (isset($errormsg)) { echo $errormsg; } ?></span>
</div>
</div>

谢谢,我希望somene会给我一个解决方案。

1 个答案:

答案 0 :(得分:0)

当您进入登录页面时,操作将是&#34; Login.php&#34;因为$_SERVER['PHP_SELF']

<form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform"> 
  

$ _SERVER [&#34; PHP_SELF&#34;]是一个超级全局变量,它返回当前正在执行的脚本的文件名。

您需要将操作更改为VerifyLogin,因此当您提交表单时,index()中的Verifylogin.php方法将会执行。

<form role="form" action="VerifyLogin" method="post" name="loginform">

xss_clean不再是表单验证的一部分,因此请将其删除。应在未输入的输出上使用XSS清洁。

$this->form_validation->set_rules('email', 'E-mail', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');