Codeigniter安全登录系统

时间:2015-08-17 07:05:27

标签: php codeigniter login

我在我的项目中使用了codeigniter作为php框架。有人说它在登录过程中如此脆弱,他可以轻松登录而无需密码。我刚刚在密码中使用了md5哈希来进行安全登录。我知道这还不够。任何人都可以在这方面建议我更多的安全过程吗?这是我的登录控制器

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

class Admin_login extends CI_Controller {

public function __construct()
{
    session_start();
    parent::__construct();
    $this->load->helper('form');
    $this->load->helper('url');        
    $this->load->model('admin_model', 'Admin_model', true);
}

public function index(){
        if(is_loggedin())
        {
            get_access_controller();
        }
        else
        {
            $this->load->view('login_admin', '');
        }
    }  

   function login_check()
        {
            $data['username']  = $_POST['username'];
            $data['password']  = md5($_POST['password']);            
            $adminInfo = $this->Admin_model->login_check($data); 

        if($adminInfo)
        {                
            $_SESSION['admin_id']           = $adminInfo['admin_id'];
            $_SESSION['admin_name']         = $adminInfo['admin_name'];
            $_SESSION['school_id']         = $adminInfo['school_id'];
            $_SESSION['is_superadmin']     = $adminInfo['is_superadmin'];
            $_SESSION['is_loggedin']        = 1;

            message('Loggedin successfully');
            get_access_controller();
        }
        exception('invalid login information');
        redirect('admin_login');exit;
    }

    function logout()
    {
       unset($_SESSION['admin_id'],$_SESSION['admin_name'],$_SESSION['school_id'],$_SESSION['is_loggedin'],$_SESSION['is_superadmin']); 
       message('Successfully logout');
       redirect('admin_login');exit;
    }
}

和我的登录模型

if(!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin_model extends CI_Model
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    /**For Admin Login Portion**/
  function login_check($data)
    {
        $user = $data['username'];
        $password = $data['password'];        
        $sql = "SELECT * FROM tbl_admin WHERE email = '$user' AND password = '$password' AND status = 1 LIMIT 1";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

2 个答案:

答案 0 :(得分:2)

对于密码散列,请查看PHP密码API-例如,参见password_hash()和password_verify(),如果您的PHP版本与之匹配。

然后,您必须转义从客户端获得的值,例如密码,登录:在这里,您没有对保护输入值做任何事情,而是暴露于SQL注入。 http://www.codeigniter.com/user_guide/database/queries.html#escaping-queries

答案 1 :(得分:0)

由于您在没有任何验证的情况下执行查询,因此SQL Injection攻击可能容易受到攻击。您可以使用查询绑定来提高安全性。

$sql = "SELECT * FROM tbl_admin WHERE email = ?1 AND password = ?2 AND status = 1 LIMIT 1";
$query = $this->db->query($sql, ($user, $password));