如何拒绝许多用户在codeigniter中使用相同用户登录?

时间:2017-01-11 08:21:58

标签: php mysql codeigniter frameworks

我的应用程序有问题"多用户可以记录同一个用户我需要防止这个问题我希望能帮到我 我的代码:

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

class Login extends CI_Controller {
public function index()
{
    $posts = $this->input->post();
    $name  = $this->input->post('name');
    $pass  = $this->input->post('pass');   

    if(isset($posts['submit']))
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'name ', 'trim|required');
        $this->form_validation->set_rules('pass', 'pass ', 'trim|required');    

        if($this->form_validation->run())
        {
            $this->load->model("users_model");
            $res = $this->users_model->auth($name,$pass); 
            if(!empty($res))
            {
                foreach($res as $r)
                { 
                    if($r->active == 0)
                    {
                        $err = '<h4 class="alert alert-danger"> error loggin           

2 个答案:

答案 0 :(得分:0)

要确保用户未登录两次,您可以将会话存储在database中,然后在登录时检查用户是否已登录。如果是,请删除旧用户并允许登录或显示确认是否终止剩余会话的消息。我建议的是自动进行会话expire

答案 1 :(得分:0)

我的建议是更好地为登录编写单独的控制器。您的登录控制器应如下所示,

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

class Controllername extends CI_Controller {

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

        $this->load->model("users_model");

        if(!empty($this->session->userdata('userid');))
        {
            /*if any try to login again if they already logged in make them to redirect to 
               any other page you want*/
        }
    }

    public function login()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'name ', 'trim|required');
        $this->form_validation->set_rules('pass', 'pass ', 'trim|required');

        if($this->form_validation->run())
        {

            $res = $this->users_model->auth($name,$pass); 
            if(!empty($res))
            {
                //if the userdata matches then set the session while login

                $this->session->set_userdata('userid', $res[0]->userid );

               //after login redirect the user to where you want

            } 

         }
    }

}

通过检查会话是否为空它会阻止用户再次登录。每当用户尝试登录时,检查会话是否为空在构造函数中。如果会话不存在则通常会进入登录功能。如果会话已经设置,然后重定向到您想要的任何其他页面。