how to get ip address in codeigniter?

how to get ip address in codeigniter?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Accurate IP Addresses in CodeIgniter

When building a login attempt counter or tracking user sessions, relying on $_SERVER['REMOTE_ADDR'] is often insufficient for local development environments. In your specific scenario, you observed that this variable returns an incorrect IP address when running locally. This discrepancy stems from how web servers handle localhost requests versus external connections.

On a standard production server, the client's real IP address travels through the network to the web server, arriving directly in $_SERVER['REMOTE_ADDR']. However, on your local machine, your browser connects to the server via the loopback interface (127.0.0.1). Consequently, the web server sees the connection coming from itself, not from your actual physical or local network adapter.

CodeIgniter provides a dedicated helper method designed specifically for this validation and retrieval process: $this->input->ip_address(). This function is distinct from $_SERVER['REMOTE_ADDR'] because it checks the input stream intelligently. Under the hood, it often inspects headers like HTTP_X_FORWARDED_FOR or HTTP_CLIENT_IP to determine if a proxy or reverse cache (such as Apache's mod_proxy) is sitting between the client and your application. If such intermediaries exist, CodeIgniter retrieves the original client IP rather than the proxy's local address.

In your code snippet, you attempted to manually assign the value:

echo $ip = $_SERVER['REMOTE_ADDR'];

This line explicitly grabs the raw server variable. While this works for production, it fails locally because $_SERVER['REMOTE_ADDR'] resolves to 127.0.0.1. The subsequent check using $this->input->ip_address() correctly handles the retrieval, but assigning the result of the manual grab to the variable $ip before validation is redundant and misleading.

To fix the issue in your login logic, you should rely solely on the framework's input library for consistency. Here is how you should structure the update statement:

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

if (!empty($ip)) {
    $this->db->update('loginattempts', array( 
        'login_attempts' => $this->session->userdata('login_attempts'), 
        'lastLogin' => date('Y-m-d H:i:s'),
        'ip' => $ip 
    ), array('login_id' => 1));
}

This approach ensures that whether you are running on a local machine or a remote server, the database records the IP address provided by the application's input handler. It abstracts away the complexities of loopback detection and proxy headers.

It is also worth noting that $_SERVER['REMOTE_ADDR'] will never be invalid in terms of format (it will always contain an IPv4 or IPv6 string), but it may not represent the true client. If you are validating whether an IP is valid for logging purposes, checking $_SERVER['REMOTE_ADDR'] against a regex pattern like ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ is technically unnecessary since the web server guarantees it contains a string. The real validation concern is ensuring the address isn't 127.0.0.1 if you are strictly requiring external IPs for security audits.

If you require robust IP geolocation or fraud detection later, consider using dedicated libraries that handle IPv6 and CIDR notation more gracefully than raw PHP checks. For further insights on handling complex session logic or input sanitization in similar frameworks like Laravel, the resource at https://laravelcompany.com offers detailed guides on secure authentication patterns. However, for CodeIgniter specifically, trusting $this->input->ip_address() is the standard best practice.

Our network