dtkmejg127475 2014-07-09 13:09
浏览 28

使用Codeigniter登录问题

I want a login solution in CodeIgniter. I want code for user login, (the user who is registered) I'm using Mysql with php and working on localhost Kindly tell me that how can I make a login controller and model for login I'm attaching my register.php so may be you understand what should I code for login.

I updated my question. Here I'm attaching my updated files

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);

            }
        }
    }

?>

Core Model where i add function 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;
      }
}

?>

this is Model of 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');
?>

Now please tell me what code should be used for successful login that fetch data from database

  • 写回答

1条回答 默认 最新

  • duanbai1027 2014-07-09 13:59
    关注

    Creating a login form, isn't too difficult. All you have to do is check the inputted values against the values in your database. This should help you;

    This would be your controller function your login form would send the values to;

    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...
            }
        }
    }
    

    Then, your "check_login_details" function would be something like this;

          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;
          }
    

    This would return true to the controller, which would then allow you to set the session and redirect the user to wherever you like...

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况