How to get the server IP with Laravel?


The server IP will be the IP address of the server you are connected to. When you are working with Laravel to get the server IP you can make use of $_SERVER['SERVER_ADDR'].

The $_SERVER variable acts as a global variable in PHP. It holds details like header information, script location, and other miscellaneous details.

Example 1

The following example retrieves the details of the $_SERVER global variable.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class StudentController extends Controller { public function index() { print_r($_SERVER); } }

Output

On executing this will generate the following output −

Array (
   [DOCUMENT_ROOT] => C:\xampp\htdocs\laraveltest\public
   [REMOTE_ADDR] => 127.0.0.1
   [REMOTE_PORT] => 61705
   [SERVER_SOFTWARE] => PHP 7.3.33 Development Server
   [SERVER_PROTOCOL] => HTTP/1.1
   [SERVER_ADDR] => 127.0.0.1
   [SERVER_NAME] => 127.0.0.1
   [SERVER_PORT] => 8000
   [REQUEST_URI] => /test
   [REQUEST_METHOD] => GET
   [SCRIPT_NAME] => /index.php
   [SCRIPT_FILENAME] => C:\xampp\htdocs\laraveltest\public\index.php
   [PATH_INFO] => /test
   [PHP_SELF] => /index.php/test
   [HTTP_HOST] => 127.0.0.1:8000
   [HTTP_CONNECTION] => keep-alive
   [HTTP_CACHE_CONTROL] => max-age=0
   [HTTP_SEC_CH_UA] => " Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"
   [HTTP_SEC_CH_UA_MOBILE] => ?0
   [HTTP_SEC_CH_UA_PLATFORM] => "Windows"
   [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
   [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36
   [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
   [HTTP_SEC_FETCH_SITE] => none
   [HTTP_SEC_FETCH_MODE] => navigate
   [HTTP_SEC_FETCH_USER] => ?1
   [HTTP_SEC_FETCH_DEST] => document
   [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
   [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.9
   [HTTP_COOKIE] => XSRF-TOKEN=eyJpdiI6IlJCKzMwdE9FeGhnck1ycllPOHh2RGc9PSIsInZhbHVlIjoiUUw0cWdGeHlXeStSY3pQcWFGTmI5YnpaaDdpanFYSW1lUkNzNUxNTDdnUk5BT0ZjdHVqK1YxM256aUN4Z0hJTGdObytoYUlKTjVpZ0ttU3pEbGlxdXd0Z3JsMDA2bGF3YnBRTUoxb0FUVklRekVmMHNVRmk4TUFwbG4vMzlNMU4iLCJtYWMiOiI5MmZmYTkxYzhiOWFmNTdkNTJiZjQyZmFhYWQyZTgyMTM2M2NmNGFkNzdkNjA0NTY3M2QwYTk2OTI1NzE4OTlkIiwidGFnIjoiIn0%3D; laravel_session=eyJpdiI6IlZGcmxOZ0xUcnF1bUh2K2pqV1hraWc9PSIsInZhbHVlIjoiSCtqbFErN0lxUksyNzBFZkxZa3p1Q3RHRzZVVDcrd1JZRDJsaEh6eW5RNWFJUS85NzFtb21ray95bXZuZDY4dU52TWxUUVJacURCTy9ERjV2VTBiS2FFdFFsQ2MrclFkejBPaG5lNDlyNGV5M253R2o2UGZJRmxmRjgrVmV5NFIiLCJtYWMiOiJlMGU3ZWY5YTUxZjY3ZjEzNzc0MzViMTUwYTgxYmJmNjY4N2I2NmM3NjJjNzkwMGEyZDhlNmYxY2EwMDkxYjE4IiwidGFnIjoiIn0%3D
   [REQUEST_TIME_FLOAT] => 1653728150.9227
   [REQUEST_TIME] => 1653728150
)

The output from $_SERVER global variable has SERVER_NAME, SERVER_ADDR, SERVER_PORT, etc.

Example 2

Following is an example to fetch server IP using $_SERVER[‘SERVER_ADDR’] –

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Collection; class UserController extends Controller { public function index() { echo $_SERVER['SERVER_ADDR']; } }

Output

127.0.0.1

Using the SERVER_ADDR

Besides using the global $_SERVER variable, Laravel provides a request() object which can return the server IP using SERVER_ADDR as the key. You can also make use of ip() method using request() as shown in the examples below

Example 1

Following example passes the value SERVER_ADDR to the request()->server() method.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function index() { echo request()->server('SERVER_ADDR'); } }

Output

127.0.0.1

Example 2

The following example is trying to retrieve IP address using the request()->ip() method.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; //use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; class StudentController extends Controller { public function index() { echo request()->ip(); } }

Output

The output of the above code is -

127.0.0.1

Updated on: 29-Aug-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements