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_ADDR
reliability)
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_ADDR
reliability)
You can use request object:
request()->server('SERVER_ADDR');
Or you can use standard PHP way:
$_SERVER['SERVER_ADDR'];
Request::server('SERVER_ADDR')
:)
URL Reference: https://laravel.com/api/5.3/Illuminate/Http/Request.html
$_SERVER['SERVER_ADDR']; for server ip
$_SERVER['SERVER_PORT']; for server port
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']
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');