Search code examples
javascriptphplan

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


I have an website hosted on local server in my LAN network. This server is exposed outside by port forwarding from router. I want to know if a client connects from outside the LAN or inside the LAN


Solution

  • 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.