2016-12-10 15 views
0

codeigniterにログインフォームを作成しましたが、データベースに登録したユーザーの資格情報でログインすると失敗します(「無効なユーザー名とパスワード」)。私はこの問題を解決しようとしています、助けてください! 私のコントローラは以下の通りです:login.phpcodeigniterのユーザー名とパスワードが無効

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

class login extends CI_Controller 
{ 

public function __construct() 
{ 
     parent::__construct(); 
     $this->load->library('session'); 
     $this->load->helper('form'); 
     $this->load->helper('url'); 
     $this->load->helper('html'); 
     $this->load->database(); 
     $this->load->library('form_validation'); 
     //load the login model 
     $this->load->model('login_model'); 
    } 

    public function index() 
    { 
     //get the posted values 
     $username = $this->input->post("txt_username"); 
     $password = $this->input->post("txt_password"); 

     //set validations 
     $this->form_validation->set_rules("txt_username", "Username"); 
     $this->form_validation->set_rules("txt_password", "Password"); 

     if ($this->form_validation->run() == FALSE) 
     { 
      //validation fails 
      $this->load->view('login_view'); 
     } 
     else 
     { 
      //validation succeeds 
      if ($this->input->post('btn_login') == "Login") 
      { 
       //check if username and password is correct 
       $usr_result = $this->login_model->get_user($username,    
       $password); 
       if ($usr_result > 0) //active user record is present 
       { 
        //set the session variables 
        $sessiondata = array(
          'username' => $username, 
          'loginuser' => TRUE 
        ); 
        $this->session->set_userdata($sessiondata); 
        redirect("index"); 
       } 
       else 
       { 
        $this->session->set_flashdata('msg', '<div class="alert 

      alert-danger text-center">Invalid username and password!</div>'); 
        redirect('login/index'); 
       } 
      } 
      else 
      { 
       redirect('login/index'); 
      } 
      } 
     } 
     }?> 

マイモデル:login_model.php

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

    class login_model extends CI_Model 
    { 
    function __construct() 
    { 
     // Call the Model constructor 
     parent::__construct(); 
    } 

    //get the username & password from tbl_usrs 
    function get_user($usr, $pwd) 
    { 
     $sql = "select * from tbl_users where username = '" . $usr . "' and 
     password = '" . md5($pwd) . "' and status = 'active'"; 
     $query = $this->db->query($sql); 
     //echo $sql; exit; 
     return $query->num_rows(); 

    } 

    }?> 

マイビュー:login_view.phpあなたのコントローラーで

<!DOCTYPE html> 
    <html> 
    <head> 
    <meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Login Form</title> 
<!--link the bootstrap css file--> 
<link   

href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"  
rel="stylesheet"> 

    <style type="text/css"> 
    .colbox { 
     margin-left: 0px; 
     margin-right: 0px; 
    } 
    </style> 
    </head> 
    <body> 
    <div class="container" style="margin-top: 100px;"> 
    <div class="row"> 
     <div class="col-lg-6 col-sm-6"> 
      <h1>Welcome!</h1> 
     </div> 
     <div class="col-lg-6 col-sm-6">  
     <ul class="nav nav-pills pull-right" style="margin-top:20px"> 
       <li class="active"><a href="#">Login</a></li> 

      </ul> 
     </div> 
     </div> 
     </div> 
     <hr/> 

    <div class="container"> 
    <div class="row"> 
     <div class="col-lg-4 col-sm-4 well"> 
     <?php 
     $attributes = array("class" => "form-horizontal", "id" => "loginform",  

     "name" => "loginform"); 
     echo form_open("login/index", $attributes);?> 
     <fieldset> 
      <legend>Login</legend> 
      <div class="form-group"> 
      <div class="row colbox"> 
      <div class="col-lg-4 col-sm-4"> 
       <label for="txt_username" class="control- 
      label">Username</label> 
      </div> 
      <div class="col-lg-8 col-sm-8"> 
       <input class="form-control" id="txt_username" 
    name="txt_username" placeholder="Username" type="text" value="<?php echo  
    set_value('txt_username'); ?>" /> 
       <span class="text-danger"><?php echo 
    -form_error('txt_username'); ?></span> 
      </div> 
      </div> 
      </div> 

      <div class="form-group"> 
      <div class="row colbox"> 
      <div class="col-lg-4 col-sm-4"> 
      <label for="txt_password" class="control-label">Password</label> 
      </div> 
      <div class="col-lg-8 col-sm-8"> 
       <input class="form-control" id="txt_password" 
    name="txt_password" placeholder="Password" type="password" value="<?php 
    echo set_value('txt_password'); ?>" /> 
       <span class="text-danger"><?php echo  
    form_error('txt_password'); ?></span> 
      </div> 
      </div> 
      </div> 

      <div class="form-group"> 
      <div class="col-lg-12 col-sm-12 text-center"> 
       <input id="btn_login" name="btn_login" type="submit" 
      class="btn btn-default" value="Login" /> 
       <input id="btn_cancel" name="btn_cancel" type="reset" 
      class="btn btn-default" value="Cancel" /> 
      </div> 
      </div> 
     </fieldset> 
     <?php echo form_close(); ?> 
     <?php echo $this->session->flashdata('msg'); ?> 
     </div> 
     </div> 
     </div> 

    <!--load jQuery library--> 
    <script  

    src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
    </script> 
    <!--load bootstrap.js--> 
    <script 
    src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"> 

    </script> 
    </body> 
    </html> 
+0

注:?あなたは '>' https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-とコントローラとモデルを閉じる必要はありません。 tag – user4419336

+0

また、パスワードとしてMD5を使用しないでください。代わりにhttp://php.net/manual/en/function.password-hash.phpとhttp://php.net/manual/en/functionをご使用ください。 password-verify.php – user4419336

+0

4つの提案をありがとう、私のコードでエラーが見つかりましたか?なぜログインできないのですか? @ wolfgang1983 –

答えて

0

の最初のファイル名あなたのクラスは首都でなければなりません。

class Login extends CI_Controller 
独自の認証システムを作成する

そして、あなたのコントローラファイルを保存Login.phpないlogin.php

+0

クラス名の競合は、codeigniterバージョン3.Xでのみ発生します。また、ローカルマシン上で実行中にエラーをスローしません。 –

+0

'$ usr_result> 0'はエラーではありません。彼は行数を返していますので、' count() 'を取る必要はありません。 –

+0

ログイン後、次のページは404エラーページです。私はまだユーザーページを作成していない、そのエラーは正常ですか? @Hikmat Sijapati –

0

は、時間の無駄を車輪の再発明され、それが安全ではありません。それは非常にうまくいっているcodeigniter 3のクラスがあります。その名前はイオン認証です。

http://benedmunds.com/ion_auth/

0

GET_USERはおそらく呼び出されることはありません!

"class login_model extends CI_Model"は "Login_model extends CI_Model"とする必要があります。これは要件です。

ここで、Model_nameはクラスの名前です。クラス名の最初の文字は で、残りの文字は小文字にする必要があります。 クラスが基本Modelクラスを拡張していることを確認してください。

https://www.codeigniter.com/userguide3/general/models.html

関連する問題