Php: How to Check If the Client Is Local

PHP: how to check if the client is local?

"Foolproof," as always, can be tricky.

If we do restrict ourselves to IPv4, then checking for "127.0.0.1" takes care of the localhost case, but checking against "192.168." is plain wrong - it will only work if the script is being run on a server which happens to be on the 192.168 network, using a 16-bit subnet mask.

Checking $_SERVER['REMOTE_ADDR'] against $_SERVER['SERVER_ADDR'] would be a better bet. This still doesn't take care of the case of a multi-homed host (ie one which has several IP addresses in addition to 127.0.0.1), though.

In order to catch all same-network cases, you'd need to check the combination of SERVER_ADDR and subnet mask against REMOTE_ADDR, but the subnet mask isn't available in $_SERVER.

BUT I found a function which does pretty much what you want here. It's a couple of screens down and it's called clientInSameSubnet. Not my code, but looks right.

Javascript/PHP - find if the client is in the same network

Any request to your server that has been port forwarded will appear to originate from the IP address of the device that performed the port forwarding.

So you just need to test:

if ($_SERVER['REMOTE_ADDR'] == $the_ip_address_of_the_router_on_the_lan)

where $the_ip_address_of_the_router_on_the_lan is a known value such as 192.168.0.1.

Devices on the LAN, assuming they access your server directly, will make requests that appear to originate from their own LAN IP address.

Warning: If the server is accessed using a hostname (such as foo.example.com), and you use the same hostname both inside and outside the LAN, and the DNS inside the LAN points that hostname to the Internet facing ip address of the router, then the requests will still be port forwarded by the router.

How can I detect if the user is on localhost in PHP?

You can also use $_SERVER['REMOTE_ADDR'] for which IP address of the client requesting is given by the web server.

$whitelist = array(
'127.0.0.1',
'::1'
);

if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
// not valid
}

How to check if host is reachable on local network with php

look at this, https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2

its means the file you are looking for where moved to another url...

it will give you the url so you can make another request.

your code also needs to consider where the function returns false...

Simplest way to detect client locale in PHP

Not guaranteed, but most browsers submit an Accept-Language HTTP header that specifies en-us if they're from the US. Some older browsers only said they are en, though. And not all machines are set up correctly to indicate which locale they prefer. But it's a good first guess.

English-UK based-users usually set their system or user locale to English-UK, which in default browser configurations should result in en-gb as the Accept Language header. (An earlier version of this said en-uk; that was a typo, sorry.) Other countries also have en locales, such as en-za (south africa), and, primarily theoretically, combinations like en-jp are also possible.

Geo-IP based guesses will less likely be correct on the preferred language/locale, however. Google thinks that content-negotiation based on IP address geolocation makes sense, which really annoys me when I'm in Japan or Korea...

How to check if the php script is running on a local server?

Check $_SERVER['REMOTE_ADDR']=='127.0.0.1'. This will only be true if running locally. Be aware that this means local to the server as well. So if you have any scripts running on the server which make requests to your PHP pages, they will satisfy this condition too.

How do I determine if the client is accessing the website through intranet or internet?

Proxy? I suppose you mean a reverse-proxy. If so, many reverse proxies add extra http headers to the original request. Check if your request headers contain "X-Forwarded-For" http://en.wikipedia.org/wiki/X-Forwarded-For

In general, the last item is what you're looking for.

A C# example:

string xff = request.Headers["X-Forwarded-For"];
if (xff != null)
{
string[] clientIPList = xff.Split(',');
string ip = clientIPList.Last();
}


Related Topics



Leave a reply



Submit