How to get Local IP Address of System in CodeIgniter 4?

Reading Time: 5 minutes
11,596 Views

Inside this article we will see How to Get Local IP Address of System in CodeIgniter. To get Local IP address in PHP, we have several global server variables provided by PHP itself. Article contains a classified information about finding IP address of client system.

We will create a helper function, load it and use it anywhere inside the application either inside any of views or inside into controllers.

Learn More –

Let’s get started.


Get Client IP Address in PHP

We have a very simple code in PHP by the help of that we can easily get that. Have a look –

<?php
  
  echo $ip = $_SERVER['REMOTE_ADDR'];

?>

Also if we want value of client IP to be more precised, we can add few check points to get client IP address.

<?php

  function getClientIpAddress()
  {
      if (!empty($_SERVER['HTTP_CLIENT_IP']))   //Checking IP From Shared Internet
      {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
      }
      elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //To Check IP is Pass From Proxy
      {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      }
      else
      {
        $ip = $_SERVER['REMOTE_ADDR'];
      }

      return $ip;
  }

  echo getClientIpAddress();
  
?>

Let’s see the things in CodeIgniter 4 by making a custom helper.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Create CodeIgniter 4 Helper

Helpers contain standalone functions which can be used through out application to complete any specific task.

To create CodeIgniter 4 helper, need to create a PHP file into folder /app/Helpers. Let’s create a file named as app_helper.php. Keep in mind we have concatenated _helper with the name of helper file.

// Checking function already exists or not
if(!function_exists("getClientIpAddress")){
  
    function getClientIpAddress()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //Checking IP From Shared Internet
        {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //To Check IP is Pass From Proxy
        {
          $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip = $_SERVER['REMOTE_ADDR'];
        }

        return $ip;
    }
}

We have created a function inside that helper file. To use this, first we need to load and then we can freely use it.


Load CodeIgniter 4 Helper

We have two options available to load helpers in application. Either we can use BaseController.php means parent controller or we can load into individual controller.

//...
// find $helpers into controller and load custom helper

protected $helpers = ["app"]; 

//...

Keep in mind, we haven’t included _helper while loading into application.

If we want to load in any specific individual controller, need to use helper() function.

<?php

namespace App\Controllers;

class Site extends BaseController
{
  public function get_client_ip_address()
  {
    helper(["app"]);
    
    echo getClientIpAddress();
  }
}

Create Route

Open Routes.php from /app/Config folder.

//...

$routes->get("my-ip", "Site::get_client_ip_address");

//...

Application Testing

Back to project terminal and type this command to start development server

$ php spark serve

URL: http://localhost:8080/my-ip

Output

127.0.0.1


View – Using Helper to Get Client IP

After loading helper into application. Now we have an access to use it in a free way. If we are in any view file, simply we can use function.

In view file –

<?= getClientIpAddress(); ?>

We hope this article helped you to learn about to get Local IP Address of System in CodeIgniter 4 in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more