php 如何在codeigniter中获取IP地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24266212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:14:49  来源:igfitidea点击:

how to get ip address in codeigniter?

phpcodeigniterip-address

提问by user3680002

I am doing this following code for login attempt & i want get IP address of my local machine..

我正在执行以下登录尝试代码,我想获取本地计算机的 IP 地址。

if ( !$this->session->userdata('login_attempts') )
{
   $this->session->set_userdata('login_attempts', 0);
}
$attempts = ($this->session->userdata('login_attempts') + 1);
$this->session->set_userdata('login_attempts', $attempts);
// Check if the session.
if ( $this->session->userdata('login_attempts') > 4 )
{
    echo 'hi....login attempt is over';
}
// Failed. So, update the session    

echo $ip = $_SERVER['REMOTE_ADDR'];
// $ip_address = $this->input->ip_address1();
// return $ip_address;
echo $this->input->ip_address();
if ( ! $this->input->valid_ip($ip))
{
    echo 'Not Valid';
}
else
{
    echo 'Valid';
}
$this->db->update('loginattempts',array( 'login_attempts' =>$this->session->userdata('login_attempts') , 'lastLogin' =>date('Y-m-d H:i:s'),'ip'=>$ip = $_SERVER['REMOTE_ADDR'] ),array('login_id' =>1) );
echo ('hi....login attempt is'.$this->session->userdata('login_attempts'));

}

but it show incorrect ip address of my local machine.

但它显示了我本地机器的错误 IP 地址。

回答by Dan

Use $this->input->ip_address();

$this->input->ip_address();

Codeigniter Input User Guide (ip_address)

Codeigniter 输入用户指南 (ip_address)

Also, don't echo an equation. echo on another line instead:

另外,不要重复方程。在另一行回显:

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

回答by Ryan

To get the remote IP address, in PHP, you could use $_SERVER['REMOTE_ADDR'], CodeIgniter is doing the same thing behind the scenes.

要获取远程 IP 地址,在 PHP 中,您可以使用$_SERVER['REMOTE_ADDR'],CodeIgniter 在幕后做同样的事情。

回答by M.Chithirai Perumal

Controller

控制器

$data['ip'] = $this->input->ip_address();

view

看法

<?= $ip ;?>

回答by BALAJI R

use anyone from below .. same like $_SERVER['REMOTE_ADDR']

使用下面的任何人.. 与 $_SERVER['REMOTE_ADDR'] 一样

$_SERVER['HTTP_X_FORWARDED_FOR']

$_SERVER['HTTP_CF_CONNECTING_IP']

We should use codeigniter function for security, we should use $this->input->ip_address();, This will give client ip

我们应该使用 codeigniter 函数来保证安全,我们应该使用$this->input->ip_address();, 这会给客户端 ip

If you want to access server variable use like this

如果要访问服务器变量,请像这样使用

$this->input->server(array('HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR')));

For reference please have a look https://www.codeigniter.com/user_guide/libraries/input.html

供参考,请查看 https://www.codeigniter.com/user_guide/libraries/input.html