Identify if request is coming from local network (intranet)

后端 未结 4 1541
名媛妹妹
名媛妹妹 2021-02-06 02:43

I need to identify if a request comes from Internet or Intranet using either client-side or server-side.

The problem I\'m trying to solve is: our web site can be accesse

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 03:27

    You can check the ip address of a user. Private ip4 address always start with either 10., or 172., or 192.* ... more info on private networks here.

    You can also make Google Analytics load Asynchronous.

    ***************** UPDATE - PLEASE READ *************************************

    As @igor-turman has correctly pointed out, that only a portion of the "172" and the "192" address ranges are designated for private use. The remaining ip addresses starting with 172 and 192 ARE PUBLIC.

    Here is the regex expression to check for private IP addresses:

    (^192\.168\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)|(^172\.([1][6-9]|[2][0-9]|[3][0-1])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)|(^10\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)
    

    You can test this regex on regexpal.com here.

提交回复
热议问题