[英]How can I check my ip address?
I have a login function where I am using user name and password to login. 我有一个登录功能,使用用户名和密码登录。 Now I want to check the id address also.My ip addresses are in another table where id is matched with the rank. 现在我还要检查id地址,我的ip地址在另一个表中,其中id与等级匹配。 I need to know how can I check ip in same function of my controller.How can I write my functions ? 我需要知道如何在控制器的同一功能中检查ip。如何编写功能?
Ip address table: login table:
id ip_address ID uname pass rank
2 98.231.50.890 1 admin admin321 1
Here is my login function. 这是我的登录功能。 There is a filed rank. 有一个提成的等级。 IF rank is 1. User can login easily other wise function will check that ip address. 如果等级为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;
Step 1 , write an get ip address function like following 步骤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;
}
Step 2 , Query all ip_address as an array from ip address table 步骤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;
}
}
For you case i suggest you convert ip to long type before stored into database , because it will improve the query performance when ip tables have the large data. 对于您的情况,我建议您先将ip转换为long类型,然后再存储到数据库中,因为当ip表包含大量数据时,它将提高查询性能。
$long = ip2long($ip);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.