29

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.

1
  • Are you really looking for the IP address of your local machine, or of the user trying to connect? Jun 17, 2014 at 16:14

4 Answers 4

85

Use $this->input->ip_address()

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

Or in CI4: $this->request->getIPAddress()

$ip = $this->request->getIPAddress();
0
4

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

1
1

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

0

You can use on an external service if you trust that it will have 100% uptime.

$ip = file_get_contents('https://api.ipify.org');

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.