我是CodeIgniter的新手。我打算建立一个非常简单的注册/登录系统用于测试,但我从未通过注册的电子邮件和密码登录。
这是我的Login_controller:
<?php
class Login_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index($msg = NULL) {
$data['msg'] = $msg;
$this->load->view('login_view', $data);
}
public function process() {
// Load the model
$this->load->model('login_model');
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result) {
// If not valid user, then show them login page again
$msg = 'Invalid email and/or password';
$this->index($msg);
}
else {
// If valid user, go to homepage
redirect('home_view');
}
}
}
我的Login_model:
class Login_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function validate() {
// Get user input
$email = $this->input->post('email');
$password = $this->input->post('password');
// Prepare the query
$this->db->select();
$this->db->from('user');
$this->db->where('email', $email);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get();
// Check result
if($query->num_rows > 0) {
// If there is a user then create session data
$row = $query->row();
$data = array(
'id' => $row->id,
'username' => $row->username,
'email' => $row->email,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
else {
// If the previous process did not validate then return false
return false;
}
}
}
我的Login_view:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1> Login Zokzak </h1>
<?php
echo form_open('Login_controller/process');
?>
Email: <input type="email" name="email"/> <br> <br>
Password: <input type="password" name="password"/> <br>
<input type="submit" value="Login"/>
<?php
if(!is_null($msg)) {
echo $msg;
}
?>
</body>
</html>
提前谢谢!
答案 0 :(得分:0)
首先,您必须在加载视图和重定向页面之间有所区别。如果home_view
为APPPATH.'views'
目录,则文件redirect('home_view');
将无效。 Redirect function接受REQUEST_URI字符串 - 例如redirect('controller_name/method_name/param1/param2')
。查看文件由CI加载程序加载。要创建消息,您需要使用下次请求时可用的flash session。
if(! $result) {
// If not valid user, then show them login page again
$this->session->set_flashdata('msg', 'Invalid email and/or password');
redirect('login_controller');//index method is loaded by default
}
else
{
$this->session->set_flashdata('msg', 'Successfully Logged In.');
redirect('top_secret_controller/admin_dashboard_method');
}
在Top_secret_controller.php
:
class Top_secret_controller extends CI_Controller
{
private $logged_in = FALSE;
public function __construct()
{
parent::__construct();
$this->logged_in = ( isset($_SESSION['validated']) && $_SESSION['validated'] === 1 ) ? TRUE : $this->logged_in;//in your model use integer for validated value instead TRUE to [avoid confusing](https://dev.mysql.com/doc/refman/5.7/en/boolean-literals.html) with converting to integer since in DB it will be writen as 1
$this->logged_in || redirected('login');//if not logged in redirect to login page
}
public admin_dashboard_method()
{
//code here
}
}
快速YT搜索会为您提供有关登录系统的大量视频(例如this one),以便您可以将良好做法与自己的做法进行比较。就个人而言,我正在使用Ion Auth system不想发明轮子(除非我需要一个更圆的:P)。