2014-12-04 87 views
1

我使用CodeIgniter - 截至2014年12月4日的最新版本。密码功能不允许访问

我想写一个密码功能到我的网站公司登录门户网站。门户网站目前还没有编写,只是功能。它显示并不会给我任何错误,除了“密码无效”之外。我不知道问题出在哪里。所以我包括所有的适用资源

更新!我发现问题在哪里,现在我只需要知道如何解决这个问题。在我的模型上,我使用MD5加密了密码。在运行探查器并看到从数据库或应用程序生成并发送加密密码后,我将其关闭,我不确定。无论如何,密码让我通过,并在需要时拒绝访问。我将如何保持加密?

---- ----模型 Company_user

<?php 

class Company_user extends CI_Model { 
    function login($username, $password){ 
     $this->db->select('company_user_id, username, password'); 
     $this->db->from('company_user'); 
     $this->db->where('username', $username); 
     $this->db->where('password', MD5($password)); 
     $this->db->limit(1); 

     $query = $this->db->get(); 

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

---- ---- CONTROLLERS

登录

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

class Login extends CI_Controller { 
    function __construct() { 
     parent::__construct(); 
    } 

    public function index() { 
     $this->load->helper(array('form')); 
     $title = 'ImpactU Online'; 
     $subtitle = 'Company Login Portal'; 
     $subhead = 'Login Below'; 
     $this->load->view('template/header', array(
      'title' => $title, 
      'subtitle' => $subtitle, 
      'subhead' => $subhead, 
     )); 
     $attributes = array(
      'class' => 'pure-form pure-form-stacked alert alert-info', 
     ); 
     $this->load->view('login_view', array(
      'attributes' => $attributes, 
     )); 
     $this->load->view('submit'); 
     $this->load->view('template/footer'); 
    } 
} 

Verfiylogin

 <?php 

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

class Verifylogin extends CI_Controller { 

    function __construct() { 
     parent::__construct(); 
     $this->load->model('company_user', '', TRUE); 
    } 

    function index() { 
     //This method will have the credentials validation 
     $this->load->library('form_validation'); 

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

     if ($this->form_validation->run() == FALSE) { 
      //Field validation failed. User redirected to login page 
      $title = 'ImpactU Online'; 
      $subtitle = 'Company Login'; 
      $subhead = 'Login Below.'; 
      $attributes = array(
       'class' => 'pure-form pure-form-stacked alert alert-info', 
      ); 
      $this->load->view('template/header', array(
       'title' => $title, 
       'subtitle' => $subtitle, 
       'subhead' => $subhead, 
      )); 
      $this->load->view('login_view', array(
       'attributes' => $attributes, 
      )); 
      $this->load->view('submit'); 
      $this->load->view('template/footer'); 
     } else { 
      //Go to private area 
      redirect('company_home', 'refresh'); 
     } 
    } 

    function check_database($password) { 
     //Field validation succeeded. Validate against database 
     $username = $this->input->post('username'); 

     //query the database 
     $result = $this->company_user->login($username, $password); 

     if ($result) { 
      $sess_array = array(); 
      foreach ($result as $row) { 
       $sess_array = array(
        'id' => $row->id, 
        'username' => $row->username 
       ); 
       $this->session->set_userdata('logged_in', $sess_array); 
      } 
      return TRUE; 
     } else { 
      $this->form_validation->set_message('check_database', 'Invalid username or password'); 
      return false; 
     } 
    } 

} 

Company_home

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
session_start(); //we need to call PHP's session object to access it through CI 
class Company_home extends CI_Controller { 

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

function index() 
{ 
    if($this->session->userdata('logged_in')) 
    { 
    $session_data = $this->session->userdata('logged_in'); 
    $title = 'ImpactU Online'; 
    $subtitle = 'Welcome To the Company Portal'; 
    $subhead = 'Please Select an Option'; 
    $this->load->view('template/header', array(
     'title' => $title, 
     'subtitle' => $subtitle, 
     'subhead' => $subhead, 
    )); 
    $data['username'] = $session_data['username']; 
    $this->load->view('company_home_view', $data); 
    $this->load->view('template/footer'); 
    } 
    else 
    { 
    //If no session, redirect to login page 
    redirect('login', 'refresh'); 
    } 
} 

function logout() 
{ 
    $this->session->unset_userdata('logged_in'); 
    session_destroy(); 
    redirect('company_home', 'refresh'); 
} 

} 

---- ---- VIEWS

模板/报头

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"/> 
     <title><?php echo html_escape($title); ?></title> 
     <link rel="shortcut icon" href="<?php echo base_url("assets/images/favicon.ico"); ?>" type="image/x-icon"> 
     <link rel="icon" href="<?php echo base_url("assets/images/favicon.ico"); ?>" type="image/x-icon"> 
     <link 
      href="<?php 
      echo base_url('assets/css/impactU.css'); 
      ?>" rel="stylesheet" type="text/css" 
      /> 
     <link 
      href="<?php 
      echo base_url('assets/font-awesome-4.2.0/css/font-awesome.min.css'); 
      ?>" rel="stylesheet" type="text/css" 
      /> 
     <link 
      href="<?php 
      echo base_url('assets/bootstrap/css/bootstrap.min.css'); 
      ?>" rel="stylesheet" type="text/css" 
      /> 
     <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css"> 
     <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-min.css"> 
     <link 
      href="<?php 
      echo base_url('assets/css/side-menu.css'); 
      ?>" rel="stylesheet" type="text/css" 
      /> 
     <script> 
      (function (i, s, o, g, r, a, m) { 
       i['GoogleAnalyticsObject'] = r; 
       i[r] = i[r] || function() { 
        (i[r].q = i[r].q || []).push(arguments) 
       }, i[r].l = 1 * new Date(); 
       a = s.createElement(o), 
         m = s.getElementsByTagName(o)[0]; 
       a.async = 1; 
       a.src = g; 
       m.parentNode.insertBefore(a, m) 
      })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); 

      ga('create', 'UA-57039794-1', 'auto'); 
      ga('send', 'pageview'); 

     </script> 
    </head> 
    <body> 
     <div id="layout"> 
      <!-- Menu toggle --> 
      <a href="#menu" id="menuLink" class="menu-link"> 
       <!-- Hamburger icon --> 
       <span></span> 
      </a> 

      <div id="menu"> 
       <div class="pure-menu pure-menu-open"> 
        <a class="pure-menu-heading" href="<?php echo site_url(); ?>">ImpactU</a> 

        <ul> 
         <li><a href="<?php echo site_url(); ?>"> 
           <i class="fa fa-home"></i> 
           Home 
          </a> 
         </li> 
         <li><a href="<?php echo base_url('index.php/blog'); ?>"> 
           <i class="fa fa-rss"></i> 
           Blog 
          </a> 
         </li> 
         <li><a href="<?php echo base_url('index.php/store'); ?>"> 
           <i class="fa fa-money"></i> 
           Store 
          </a> 
         </li> 
         <li><a href="<?php echo base_url('index.php/contact'); ?>"> 
           <i class="fa fa-envelope"></i> 
           Contact 
          </a> 
         </li> 
         <li><a href="<?php echo base_url('index.php/about'); ?>"> 
           <i class="fa fa-exclamation-circle"></i> 
           About 
          </a> 
         </li> 
         <li class="menu-item-divided"><a href="<?php echo base_url('index.php/login'); ?>"> 
           <i class="fa fa-lock"></i> 
           Company Login 
          </a> 
         </li> 
         <li class="menu-item-divided"><a href="<?php echo base_url('index.php/paypal'); ?>"> 
           <i class="fa fa-paypal"></i> 
           Paypal Demo 
          </a> 
         </li> 
        </ul> 
       </div> 
      </div> 

      <div id="main"> 
       <div class="header"> 
        <h1><?php echo html_escape($title); ?></h1> 
        <h2><?php echo html_escape($subtitle); ?></h2> 
       </div> 

       <div class="content"> 
        <h2 class="content-subhead"><?php echo html_escape($subhead); ?></h2> 

模板/页脚

<hr/> 
<div class="footer"> 
    <p><i class="fa fa-copyright"></i> 2014 Tyler Lazenby</p> 
</div> 
</div> 
</div> 
</div> 


<script src="<?php $this->load->helper('url'); 
echo base_url('assets/js/ui.js'); ?>"> 
    </script> 
</body> 
</html> 

login_view

<?php echo validation_errors(); ?> 
<?php echo form_open('c=verifylogin', $attributes); ?> 
<div class="pure-g"> 
    <div class="pure-u-1 pure-u-md-1-3"> 
     <label for="username">Username</label> 
     <input id="username" type="text" name="username" placeholder="username" value="<?php echo set_value('username'); ?>" required/> 
    </div> 
    <div class="pure-u-1 pure-u-md-1-3"> 
     <label for = "password">Password</label> 
     <input id="password" type="password" name="password" required/> 
    </div> 
</div> 

提交

<legend>Click submit when done</legend> 
<div> 
    <button type="submit" class="pure-button pure-button-primary"> 
     <i class="fa fa-thumbs-o-up"></i> 
     Submit 
    </button> 
</form> 
</div> 

company_home_view

<h2 class="content-subhead">Welcome <?php echo $username; ?>!</h2> 
    <a href="home/logout">Logout</a> 

你的帮助会非常赞赏。

+0

我已经找到了一个你可能会遇到的错误,但这不是解决方案; Verifylogin控制器的第55行的“id”应该是“computer_user_id”。 – 2014-12-04 21:31:13

回答

0

这非常简单,只需要将模型中的加密。

相关问题