User_log.php(这是控制器文件)
<?php
class User_log extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user_data');
}
public function index($msg = NULL)
{
$data['msg'] = $msg;
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$data['attribute'] = array('name' => 'process');
$data['data'] = array(
'name' => 'username',
'id' => 'username',
);
$data['pass'] = array(
'name' => 'password',
'id' => 'password',
);
$this->load->view('login', $data);
}
public function process()
{
// Load the model
$this->load->model('user_data');
// Validate the user can login
$result = $this->user_data->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$msg = '<font color=red>Invalid username and/or password.</font><br />';
$this->index($msg);
}else{
// If user did validate,
// Send them to members area
redirect('success');
}
}
}
User_data.php(这是模型文件)
<?php
class User_data extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function validate()
{
$username = $this->security->xss_clean($this->input- >post('username'));
$password = $this->security->xss_clean($this->input- >post('password'));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('user');
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$data = array(
'id' => $row->id,
'username' => $row->username,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
return false;
}
}
login.php(这是视图文件)
<head>
<title>Jotorres Login Screen | Welcome </title>
</head>
<body>
<div id='login_form'>
<form action='<?php echo base_url();?>index.php/blog/process' method='post' name='process'>
<h2>User Login</h2>
<br />
<label for='username'>Username</label>
<input type='text' name='username' id='username' size='25' /><br />
<label for='password'>Password</label>
<input type='password' name='password' id='password' size='25' /><br />
<input type='Submit' value='Login' />
</form>
</div>
</body>
</html>
此代码的问题在哪里?在 CodeIgniter 3.0版中无法正常工作。
答案 0 :(得分:1)
根据提供的代码,您的表单操作是错误的。
<form action='<?php echo base_url();?>index.php/blog/process' method='post' name='process'>
^ ^
这应该是
<form action='<?php echo base_url();?>index.php/user_log/process' method='post' name='process'>
^ ^
而不是blog
,它应该是user_log
。
此外,您没有在登录页面中回显错误消息。
在login.php
标记之后的<form>
中添加一些内容。
<?= $msg ?>
答案 1 :(得分:0)
第1步:
在视图中从博客更改为 user_blog
<form action='<?php echo base_url();?>index.php/user_log/process' method='post' name='process'>
第2步:
在模型中修改此功能
public function process()
{
// Load the model
$this->load->model('user_data');
// Validate the user can login
$result = $this->user_data->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$msg = '<font color="red">Invalid username and/or password.</font><br />';
$this->index($msg);
}else{
// If user did validate,
// Send them to members area
redirect('success');
}
}