15

Using Laravel, I can get the client IP with request()->ip().

Is there a Laravel built-in way to get the server IP? Or is it something "impossible" (same problem as SERVER_ADDRreliability)

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • Possible duplicate of [How to identify server IP address in PHP](http://stackoverflow.com/questions/5800927/how-to-identify-server-ip-address-in-php) – patricus Dec 21 '16 at 08:41
  • @patricus Thanks. My question was about a laravel built-in way. I did not tagged it `php` for that reason. That's why I think it's not a duplicate. – rap-2-h Dec 21 '16 at 08:51
  • The problem is that laravel is built on top of PHP. If there is no reliable way in PHP, there won't be a reliable way in laravel. But, as has been answered, there is a laravel way of accessing the $_SERVER superglobal, but as you mentioned in your question, still not reliable. – patricus Dec 21 '16 at 15:22

5 Answers5

24

You can use request object:

request()->server('SERVER_ADDR');

Or you can use standard PHP way:

$_SERVER['SERVER_ADDR'];
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
2

Request::server('SERVER_ADDR') :)

URL Reference: https://laravel.com/api/5.3/Illuminate/Http/Request.html

Imam Assidiqqi
  • 516
  • 4
  • 20
1
$_SERVER['SERVER_ADDR']; for server ip
$_SERVER['SERVER_PORT']; for server port
ujwal dhakal
  • 2,289
  • 2
  • 30
  • 50
0

If you are using IIS, you can only get the server IP using

$_SERVER['LOCAL_ADDR']

if it is LINUX, use :

$_SERVER['SERVER_ADDR'];

/** Server IP Check **/
if(!isset($_SERVER['SERVER_ADDR']){ $_SERVER['SERVER_ADDR'] = $_SERVER['LOCAL_ADDR']; }

/** Get Server IP **/
!isset($_SERVER['SERVER_ADDR']))?$_SERVER['LOCAL_ADDR']:$_SERVER['SERVER_ADDR']
Saurabh
  • 2,655
  • 1
  • 20
  • 47
xyonme
  • 395
  • 1
  • 7
  • 24
0

You can use this way on the server.

$_SERVER['SERVER_ADDR'];

But you can not access SERVER_ADDR index locally. It will through Undefined index: SERVER_ADDR error. In my case, I face this error.

A better solution would be using variable from .env file which will work locally and server as well.

Defined a variable in .env file like this. You can give any name you want.

SERVER_ADDR = 127.0.0.1

Note: Please note SERVER_ADDR value should be 127.0.0.1 or localhost to get the server address value.

Now you can access this variable like this:

env('SERVER_ADDR');