我如何检查我的ip地址


How can I check my ip address?

我有一个登录功能,使用用户名和密码登录。现在我也想检查一下身份证地址。我的ip地址在另一个表中,其中id与等级匹配。我需要知道如何在我的控制器的相同功能中检查ip。如何编写函数?

Ip address table:               login table:
id  ip_address                  ID uname pass      rank
2   98.231.50.890               1  admin admin321   1

这是我的登录功能。有一个正式的军衔。IF等级为1。用户可以轻松登录,其他功能会检查该ip地址。

public function loginAction() {
        $postArray = $this->input->post();
        $table= 'login';
        //$user_type=1;
        if (!empty($postArray)) {
            $result = $this->m_common->login_with_password_string($postArray['user_name'], $postArray['user_pass'], $table);
            if (!empty($result->ID) && isset($result->ID)) {
                $this->session->set_userdata('user_id',$result->ID);
                $this->session->set_userdata('user_name',$result->uname);
                $this->session->set_userdata('theme',$result->theme);
                $this->session->set_userdata('logged_in',1);
                //$this->is_logged_in($this->session->userdata('logged_in'));
                redirect(site_url(ADMIN.'/dashboard/view_player'));
//                echo 'Hello';
            } else {
                 $data['message']="Incorrect Username or Password";
                  $this->titlebackend("View Players");
                 $this->load->view('v_login',$data);
            }
        }
    }

你可以使用这个

$ip = $this->input->ip_address(); echo $ip;

步骤1,编写一个获取ip地址的函数,如以下

 function getRealIpAddr(){
  if(!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip = $_SERVER['HTTP_CLIENT_IP'];
  }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }else{
      $ip = $_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}

步骤2,从ip地址表中查询所有ip_address作为数组

$ips = array(); // assume this result from you database
if(in_array($ip,$ips)){
  // true
}
// or you can loop the ips array
foreach(ips as $lip){
  if($lip == $ip){
     //true
     break;
  }
}

对于您的情况,我建议您在存储到数据库之前将ip转换为long类型,因为当ip表具有大数据时,这将提高查询性能。

$long = ip2long($ip);