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

Share this Article
Reading Time: 5 minutes
659 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.

Let’s consider a CodeIgniter 4 application.

How can we do this?

Also if you don’t have any idea about custom helper in CodeIgniter you will get that too in this article.

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

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

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 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.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# 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.

//...

$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.

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.

Find More on CodeIgniter 4 here

Leave a Comment