我想在CodeIgniter中使用登录解决方案。我想要用户登录的代码,(注册的用户)我正在使用Mysql与php并在localhost上工作 请告诉我如何制作登录控制器和登录模型 我正在附上我的register.php,所以你可能理解我应该为登录编码什么。
我更新了我的问题。 这里我附上了我的更新文件
login.php(controller)
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends CI_Controller {
function index() {
$myData['pageName'] = 'login';
$this->load->helper(array('form', 'url'));
$this->load->database();
if ($_POST) {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == false) {
// Login page view...
$this->load->view('login', $myData);
}
else{
}
$this->load->model('Login');
if ($this->Login->check_login_details($this->input->post('email') , $this->input->post('password'))) {
// show success
$this->load->view('success_login', $myData);
}
}else {
// Invalid details...
$this->load->view('login', $myData);
}
}
}
?>
核心模型,其中我添加了函数check_details
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Model extends CI_Model {
const TableName = 'form';
const TablePK = 'id';
public function ins_record() {
$this->db->insert($this::TableName, $this);
}
public function getRec() {
$res = $this->db->get($this::TableName);
return $res->result_array();
}
public function get_userName($colName, $value) {
$res = $this->db->get_where($this::TableName, array($colName => $value));
return $res->result_array();
}
public function check_login_details($email, $password)
{
// Do whatever to the password...
$password = md5($password);
$query = $this->db->get_where($this::TableName, array('email' => $email, 'password' => $password));
return ($query->num_rows() > 0) ? true : false;
}
}
?>
这是login.php的模型
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends My_Model{
const TableName = 'form';
const TablePK = 'id';
public $email = '';
public $password = '';
}
?>
here is also my login.php (view code)
<?php
include('./includes/header.php');
?>
<!-- MAIN -->
<div id="main">
<!-- wrapper-main -->
<div class="wrapper">
<!-- content -->
<div id="content">
<!-- title -->
<div id="page-title">
<span class="title">Sign in Form</span>
<span class="subtitle">Kindly login here to access site's useful features</span>
</div>
<!-- ENDS title -->
<form class="form-signin" role="form" method="post">
<h2 >Please sign in</h2>
<input type="email" class="form-control" placeholder="Email address" required autofocus>
<input type="password" class="form-control" placeholder="Password" required >
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<!-- ENDS content -->
</div>
<!-- ENDS wrapper-main -->
</div>
<!-- ENDS MAIN -->
<!-- Twitter -->
<div id="twitter">
<div class="wrapper">
<a href="#" id="prev-tweet"></a>
<a href="#" id="next-tweet"></a>
<img id="bird" src="img/bird.png" alt="Tweets" />
<div id="tweets">
<ul class="tweet_list"></ul>
</div>
</div>
</div>
<!-- ENDS Twitter -->
<?php
include('./includes/footer.php');
?>
现在请告诉我应该使用哪些代码来成功登录从数据库中获取数据
答案 0 :(得分:0)
创建登录表单并不太难。您所要做的就是根据数据库中的值检查输入的值。这应该对你有帮助;
这将是您的登录表单将值发送给您的控制器功能;
function login()
{
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ( $this->form_validation->run() == false )
{
// Login page view...
}
else
{
if ( $this->Users_model->check_login_details($this->input->post('email'), $this->input->post('password')) )
{
// Login was OK....
}
else
{
// Invalid details...
}
}
}
然后,你的“check_login_details”函数会是这样的;
function check_login_details($email, $password)
{
// Do whatever to the password...
$password = md5($password);
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password));
return ($query->num_rows() > 0) ? true : false;
}
这将返回到控制器,这将允许您设置会话并将用户重定向到您喜欢的任何地方......
希望这有帮助。