How to get the server IP with Laravel - laravel

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)

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

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');

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']

This also gives you the IP
$request->ip();

Related

Laravel URL Helper return IP address instead of Domain name

I have a problem when trying to create the Url using URL helper. I'm using Laravel 6.
$verify_url = url("/verify");
This is return the URL with the IP address instead of the domain name.
I don't know if it is a problem with the Apache server or the code.
Please help me. Thanks.
Adding this directive to Apache Virtual Host config seems to have fixed it: ProxyPreserveHost On
But a better way to use your urls is to name them in your routes/web.php. For ex:
Route::post('/verify', 'HomeController#verify')->name('verify');
and wherever you need to access to this url just use like this:
$verify_url = route('verify');

Redirecting to another laravel project running on another port [Laravel]

I am relatively new in Laravel. Here is the problem I am facing-
I have two Laravel projects running on 2 different ports, one on port 8000 and another on port 5000 in localhost. I am trying to redirect from a url in port 5000 to another url which is under port 8000. I tried doing this-
return redirect('127.0.0.1:8000/sso/login');
But this results in making the url "localhost:5000/127.0.0.1:8000/sso/login" which is obviously not what I want. Is there a way to redirect to a different port in Laravel?
you have to use "http://" as part of url in redirect method.
return redirect('http://127.0.0.1:8000/sso/login');

Editing HOSTS file for specific URL?

I have tried to edit my HOSTS file to block just a specific url like so:
127.0.0.1 google.com/pagetoblock
127.0.0.1 www.google.com/pagetoblock
However that isn't working.
Does anyone know where I'm going wrong?
Your HOSTS file only allows you to set the IP address for (as the name suggests) the host (e.g. google.com or www.google.com). You cannot set the IP address for specific pages.
You could use a tool like Microsoft Fiddler to set the IP Address for specific URLs, but this would require Fiddler be running continuously.
Fiddler has a rules engine accessed by Rules → Customize Rules.
There is a great set of samples for your learning, but the following script should work.
For example, to block the logo on the http://www.google.co.uk homepage, you could use the following script:
if (oSession.url == "www.google.co.uk/images/srpr/logo3w.png"){
// Prevent this request from going through an upstream proxy
oSession.bypassGateway = true;
// Rewrite the IP address of target server
oSession["x-overrideHost"] = "127.0.0.1";
// Set the color of the request in RED in Fiddler, for easy tracing
oSession["ui-color"]="red"; 
}

Is it possible to install website made in php fox on localhost?

I have website made in php_fox framework and my client want some changes on website but he don't want that changes on his server directly so firstly i have install that website on my localhost. so is it possible to install that website on localhost??? because right now i am getting 505 internal server error :(
Thanks,
Ruchita
yes, you have just to activate rewrite_module in apache from apache module (wamp)
Please check your server.sett.php (\include\setting)
search $_CONF['core.url_rewrite']
value of this variable should be 2 at localhost
$_CONF['core.url_rewrite'] = '2';
Please check your server.sett.php (\include\setting)
change the following $_CONF['core.host'] = 'localhost';
edit this $_CONF['core.folder'] = '/'; to $_CONF['core.folder'] = '/<your_phpfox_dir>/';
and edit $_CONF['core.url_rewrite'] = '2';
Additionally configure database settings
Delete everything inside \file\cache
Now run the phpfox instance in your localhost.

Ajax get throws a syntax error when ip is given instead of localhost

i am working on cakephp.
I have developed an application where i have used localhost in all ajax post and get..
like
var ht = $.ajax({
type: "GET",
url: "http://localhost/FormBuilder/index.php/forms/getInvitees/<?php echo $emailid;?>",
async: false
}).responseText;
var myObject = eval('(' + ht + ')');
this thing works only when i put localhost . But when i change that to my Ip like
http://111.11.11.11/FormBuilder/index.php/forms/getInvitees/",
then i am getting a syntax error () in the line
var myObject = eval('(' + ht + ')');
WHy it happens ?? Please give valuable suggestions in solving this..
The response for ht will be
{"invitees":[{"invitee":"23"}]}
from which i will generate a link by
var data = myObject;
$.map(data.invitees, function(i){
var id=i.invitee;
$("<a href=<?php echo $link?>/"+id+"/Invitee> <?php echo $link?>/"+id+"/Invitee</a>").appendTo("#"+inc);
inc++;
return i.invitee;});
Thank you
You know that localhost translates to 127.0.0.1 in almost any case. Did you make sure to setup your webserver to bind to 111.11.11.11 correctly and serve the same DocumentRoot?
Looks like your not getting a JSON object back when calling the server via IP.
EDIT
I don't know whether or not you are using VirtualHosts to set up your development environment but since you're making use of localhost I will go with Apache's standard httpd.conf.
In the httpd.conf file search for a line that start with Listen .... Make sure that the only line with a Listen directive looks like Listen *:80, to allow Apache to bind itself to any of the IPs available on your machine.
Then, insert the following near the end of the file:
<VirtualHost *>
ServerName myfoo.com
ServerAdmin admin#myfoo.com
DocumentRoot "C:/..path to your working directory/"
</VirtualHost>
Now you have successfully implemented a VirtualHost that serves localhost and any other IP your machine is assigned to from the same DocumentRoot (the directory your HTML/PHP/whatever files reside in).
Good luck.
Surely using localhost is more generic in this case?
In my CakePHP apps, I use:
'http://localhost/cakeapp/nodeDescriptors/ajaxSetStatus'
or
'/cakeapp/nodes/updateTreeNodes'
for AJAX calls.
Remember you'll run into problems if you try to access a different domain directly. Perhaps that is what's happening with your IP-based call?
Localhost will only work from your local machine, but mind you, your app will eventually be accessed from out side , where localhost would be the clients machine..
You need to specify the public IP address or the domain name or the network name if you use an internal DNS for an intranet app.

Resources