I am doing this following code for login attempt & i want get IP address of my local machine..
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.
You can try this one also. $ip=$_SERVER['REMOTE_ADDR']; echo "IP address= $ip"; If your application hosted on same machine from where you are trying to request it will always return '::1', It means LocalHost. else it will return client IP Address.
Using getenv() function: To get the IP Address,we use getenv(“REMOTE_ADDR”) command. The getenv() function in PHP is used for retrieval of values of an environment variable in PHP.
Get IP Address of the User in CodeIgniter The input class provides two built-in functions namely ip_address() and valid_ip() to process the ip address. To get the ip address of the current user use it like this, $ip = $this->input->ip_address(); This returns the current user's ip address.
The request class is an object-oriented representation of an HTTP request. This is meant to work for both incoming, such as a request to the application from a browser, and outgoing requests, like would be used to send a request from the application to a third-party application.
Use $this->input->ip_address()
$ip = $this->input->ip_address();
Or in CI4: $this->request->getIPAddress()
$ip = $this->request->getIPAddress();
To get the remote IP address, in PHP, you could use $_SERVER['REMOTE_ADDR']
, CodeIgniter is doing the same thing behind the scenes.
use anyone from below .. same like $_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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With