dongyan3237 2016-03-28 12:45
浏览 69
已采纳

Codeigniter登录问题

I'm having difficulties in understanding what's going on here.

I've made a login page, setting it to go to admin page. Without any user data yet, just to check if it's working. And It doesn't go. I hade some problems loading the library form_validation. So I added the parent construct.

Controller

class Login Extends CI_Controller{
public function __construct()
{
    parent::__construct();
    $this->load->helper('url'); 

}

public function index() {

    $this->load->view('login');
    $this->load->helper('url'); 

}

public function login() {

    $this->load->library('form_validation');
    $this->load->helper('url'); 
    $this->form_validation->set_rules('username','Username','trim|required');
    $this->form_validation->set_rules('password','Password','trim|required|md5');
    if($this->form_validation->run()==false){
        $this->index();
    }else{
        $user_session=array(
            'Username'      => $this->input->post('username'),
            'Password'      => $this->input->post('password'),
            'is_logged_in'  => 1
        );
        $this->session->set_userdata($user_session);
        redirect('login/admin');
    }

}



public function admin() {
    $this->load->view('admin');

}

}

My Login View

 <section class="login_content">
        <?php echo validation_errors(); ?>
        <form action="<?php echo base_url().'login/login'; ?>" method="post">
        <h1>Login no Sistema</h1>
        <div>
            <input type="text" name="username" class="form-control" placeholder="Username" required="" />
        </div>
        <div>
            <input type="password" name="password" class="form-control" placeholder="Password" required="" />
        </div>
        <div>
            <input type="submit" name="submit" value="Login" />

        </div>
        <div class="clearfix"></div>
        <div class="separator">


          <div class="clearfix"></div>
          <br />
          <div>
            <h1> Sitio Monica e Marcia</h1>

            <p>©2016 Todos os direitos reservados. Sitio Monica e Marcia.</p>
          </div>
        </div>
      </form>
      <!-- form -->
    </section>

My Admin View

 <div id="login" class="animate form">
    <section class="login_content">
        <h1>Bem vindo ao Admin</h1>
        <?php 
            echo '<pre>';
            print_r($this->session->all_userdata());
            echo '<pre>';

        ?>
      <!-- form -->
    </section>
    <!-- content -->
  </div>
  • 写回答

4条回答 默认 最新

  • dongling2545 2016-03-28 15:15
    关注

    Here Is Your Controller

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        /* Description: Login controller class*/
     class Login extends CI_Controller{
    
    function __construct(){
        parent::__construct();
    
        $this->load->library('session');
         $this->load->model('login_model');
    
    }
    
    public function admin($msg = NULL){
        // Load our view to be displayed
        // to the user
        $data['msg'] = $msg;
        if($msg == NULL)
        {
    
        $this->load->view('login');
        }
        else
        {
            //print_r($data);
        //die();
        $this->load->view('login',$data);
        }
    }
    
     public function process(){
        // Load the model
    
        // Validate the user can login
        $result = $this->login_model->validate();
        // Now we verify the result
        if(! $result){
            // If user did not validate, then show them login page again
            $msg = 'Invalid username or password';
            $this->admin($msg);
        }else{
            // If user did validate, 
            // Send them to members area
            redirect('home/check_isvalidated');
        }        
    }
    public function doLogout(){
    
        $this->session->sess_destroy();
        redirect(base_url());
       }
     }
    

    Here IS The Model You Can Use Validation instead of Security

    public function validate(){
        // grab user input
        $username = $this->security->xss_clean($this->input->post('user_name'));
        $password = $this->security->xss_clean(md5($this->input->post('password')));
    
        // Prep the query
        $this->db->where('user_name', $username);
        $this->db->where('password', $password);
    
        // Run the query
        $query = $this->db->get('admin');
        // Let's check if there are any results
        if($query->num_rows == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
                    'id' => $row->id,
                    'user_name' => $row->user_name,
                    'validated' => true
                    );
            $this->session->set_userdata($data);
            return true;
        }
        // If the previous process did not validate
        // then return false.
        else {
        return false;
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable