Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get ip address in codeigniter?

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.

like image 662
user3680002 Avatar asked Jun 17 '14 14:06

user3680002


People also ask

How can I get IP address and store in database using PHP?

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.

How can we get the IP address of the client in PHP?

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.

How to get ip address of user in CodeIgniter?

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.

What is request in CodeIgniter?

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.


3 Answers

Use $this->input->ip_address()

$ip = $this->input->ip_address();
  • Codeigniter 2 - Input Class: ip_address()
  • CodeIgniter 3 - Input Class: ip_address()

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

$ip = $this->request->getIPAddress();
  • CodeIgniter 4 - Request Class: getIPAddress()
like image 114
Dan Avatar answered Oct 02 '22 16:10

Dan


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

like image 42
Ryan Avatar answered Oct 02 '22 18:10

Ryan


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

like image 35
BALAJI R Avatar answered Oct 02 '22 18:10

BALAJI R