Codeigniter验证始终返回false

时间:2017-05-16 06:59:26

标签: php codeigniter validation

我是编码的初学者。我在CI验证方面遇到问题,它总是返回false。This is the picture

这是我的控制器: `

//session_start();

Class Home extends CI_Controller {

    public function __construct() {
        parent::__construct();

        // Load database
        $this->load->model('login_pegawai');
    }

    // Show login page
    public function index() {
        $this->load->view('utama');
    }

    // Check for employee login process
    public function process() {

        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

        if ($this->form_validation->run() == FALSE) {
            if(isset($this->session->userdata['logged_in'])){
                $this->load->view('pegawai/dashboard_pegawai');
            } else {
                $this->load->view('utama');
            }
        } else {
            $data = array(
                'username' => ($this->input->post('username')),
                'password' => md5($this->input->post('password'))
            );
            $result = $this->login_pegawai->login($data);
            if ($result == TRUE) {

                $username = $this->input->post('username');
                $result = $this->login_pegawai->read_user_information($username);
                if ($result != false) {
                    $session_data = array(
                        'username' => $result[0]->username,
                        'email' => $result[0]->email,
                    );
                    // Add user data in session
                    $this->session->set_userdata('logged_in', $session_data);
                    $this->load->view('pegawai/dashboard_pegawai');
                }
            } else {
                $data = array(
                    'error_message' => '<font color=red>Invalid username and/or password.</font><br />'
                );
                $this->load->view('utama', $data);
            }
        }
    }
}

&GT;`

和我的模特:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Login_pegawai extends CI_Model {

    // Read data using username and password
    public function login($data) {

        $condition = "username =" . "'" . $data['username'] . "' AND " . "password =" . "'" . md5($data['password']) . "'";
        $this->db->select('*');
        $this->db->from('pegawai');
        $this->db->where($condition);
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }

    // Read data from database to show data in employee page
    public function read_user_information($username) {

        $condition = "username =" . "'" . $username . "'";
        $this->db->select('*');
        $this->db->from('pegawai');
        $this->db->where($condition);
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }
}

&GT;

最后我的观点:

<html>
<head>
<title>Bank Sampah</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="<?= base_url('assets/home/layout/styles/layout.css'); ?>" type="text/css" /></head>
<body id="top">

<div class="wrapper col1">
    <div id="header">
        <div id="logo">
            <h1><a href="<?php echo base_url('home')?>">Bank Sampah</a></h1>
            <p><strong>Reuse, Reduce, Recycle</strong></p>
        </div>
        <div id="newsletter">
            <p>Login Pegawai</p>
            <form action='<?php echo base_url('home/process'); ?>' method='post' name='process'>
                          <?php echo "<div class='error_msg'>";
                            if (isset($error_message)) {
                                echo $error_message;
                            }
                            echo validation_errors();
                            echo "</div>";
                          ?>
                    <input type="username" placeholder="Username&hellip;" id="username" name="username"  onfocus="this.value=(this.value=='Name&hellip;')? '' : this.value ;" />
                    <input type="password" placeholder="Password&hellip;" id="password" name="password" onfocus="this.value=(this.value=='Password&hellip;')? '' : this.value ;" />
                    <input type="submit" id="news_go" value="Login" />
            </form>
        </div>
        <br class="clear" />
    </div>
</div>

<div class="wrapper col2">
    <div id="topbar">
        <div id="topnav">
            <ul>
                <li class="active"><a href="<?php echo base_url('home')?>">Home</a></li>
                <li class="last"><a href="#">About</a></li>
                <li class="last"><a href="#">Help</a></li>
                <li class="last"><a href="#">Register</a></li>
                <li class="last"><a href="<?php echo base_url('login')?>">Login Nasabah & Pengepul</a></li>

            </ul>
        </div>

        <br class="clear" />
    </div>
</div>
<div class="wrapper col3">
    <div id="breadcrumb">
        <ul>
            <li class="first">You Are Here</li>
            <li>&#187;</li>
            <li><a href="#">Home</a></li>
        </ul>
    </div>
</div>
<div class="wrapper col5">
    <div id="container">
        <center><img class="gambar"/></center>
        <br><br>
        <h1>BANK SAMPAH</h1>
        <p>Bank Sampah adalah suatu tempat yang digunakan untuk mengumpulkan sampah yang sudah dipilah-pilah.</p>
        <p>Hasil dari pengumpulan sampah yang sudah dipilah akan disetorkan ke tempat pembuatan kerajinan sampah atau ke tempat pengepul sampah.</p>
        <p>Bank Sampah dikelola menggunakan sistem seperti perbankan yang dilakukan oleh petugas sukarelawan.</p>
        <p>Penyetor adalah warga yang tinggal di sekitar lokasi bank serta mendapat buku tabungan seperti menabung di bank.</p>
     <br><br>
    </div>
</div>
<div class="wrapper col6">
    <div id="footer">
        <div class="footbox">
            <h2>More Info</h2>
            <ul>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Lorem ipsum dolor</a></li>
                <li><a href="#">Suspendisse in neque</a></li>
                <li class="last"><a href="#">Praesent et eros</a></li>
            </ul>
        </div>
        <div class="footbox">
            <h2>Categories</h2>
            <ul>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Lorem ipsum dolor</a></li>
                <li><a href="#">Suspendisse in neque</a></li>
                <li class="last"><a href="#">Praesent et eros</a></li>
            </ul>
        </div>
        <div class="footbox">
            <h2>Lacus interdum</h2>
            <ul>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Lorem ipsum dolor</a></li>
                <li><a href="#">Suspendisse in neque</a></li>
                <li class="last"><a href="#">Praesent et eros</a></li>
            </ul>
        </div>
        <div class="footbox">
            <h2>Lacus interdum</h2>
            <ul>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Praesent et eros</a></li>
                <li><a href="#">Lorem ipsum dolor</a></li>
                <li><a href="#">Suspendisse in neque</a></li>
                <li class="last"><a href="#">Praesent et eros</a></li>
            </ul>
        </div>
        <br class="clear" />
    </div>
</div>
<div class="wrapper col7">
    <div id="copyright">

        <br class="clear" />
    </div>
</div>
</body>
</html>

我尝试了很多方法,但结果显示我一样。请让我知道我哪里弄错了。对不起说/写坏英语,谢谢。

1 个答案:

答案 0 :(得分:0)

将此替换为您的登录功能,当where子句中有多个条件时,也应定义第二个和第三个参数。

public function login($data) {

        $this->db->select('*');
        $this->db->from('pegawai');
        $this->db->where("username",$data['username']);
        $this->db->where("password",$data['password']);
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows()>0) {
            return true;
        } else {
            return false;
        }
    }