如何使用代码点火器限制移动设备对网站的登录访问



我正在开发Codeigniter应用程序框架,我想禁用每个用户的登录访问权限,除了移动设备上的管理员而不是桌面上的管理员?

我已经在我的控制器中尝试了用户代理库,但用户仍然通过移动设备登录?

当我在if condition中添加&& !$this->agent->is_mobile()时,用户仍然通过移动设备登录?

但是我希望登录应该适用于桌面上的用户和管理员,而管理员只能在移动设备上登录?

这里控制器

public function __construct()
 {
      parent::__construct();
      $this->load->library('session');
       $this->load->library('user_agent');
 }
 public function login() {
    $this->form_validation->set_rules('iusername', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('ipassword', 'Password', 'trim|required|xss_clean');
    $Return = array('result'=>'', 'error'=>'');
    $username = $this->input->post('iusername');
    $password = $this->input->post('ipassword');
    $Return = array('result'=>'', 'error'=>'');
    /* Server side PHP input validation */
    if($username==='') {
        $Return['error'] = "Username field is required.";
    } elseif($password===''){
        $Return['error'] = "Password field is required.";
    }
    if($Return['error']!=''){
        $this->output($Return);
    }
    $allowlist = array(
        '192.168.6.6',
    );
    $data = array(
        'username' => $username,
        'password' => $password
    );
    $result .= $this->Login_model->read_user_information($username);
    $checkusername = $result[0]->username;
    if ($result == TRUE) {
    if(!in_array($_SERVER['REMOTE_ADDR'],$allowlist) 
    && $checkusername != 'admin' && !$this->agent->is_mobile()){
         $Return['error'] = 'This login cannot be accessed from your location.';    
         $this->output($Return);    
    } 
    else {
            // Add user data in session
            $this->session->set_userdata('username', $session_data);
            $Return['result'] = 'Logged In Successfully.
            $this->output($Return);
        }   
        } else {
            $Return['error'] = "Invalid Login Credential.";
            /*Return*/
            $this->output($Return);
        }
    }

问题已修复

您的 if 条件中存在问题。 使用以下代码更新您的条件

if(in_array($_SERVER['REMOTE_ADDR'],$allowlist) && $username == 'admin' && !$this->agent->is_mobile()){
    $this->session->set_userdata('username', $session_data);
    $Return['result'] = 'Logged In Successfully'.
    $this->output($Return);
}else {
    $Return['error'] = 'This login cannot be accessed from your location.';    
    $this->output($Return);    
}

相关内容

  • 没有找到相关文章

最新更新