Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify if request is coming from local network (intranet)

Tags:

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 accessed from internet and intranet. The intranet user (user inside company), does not have access to internet. We are using Google Anylitics, when intranet user access the page, the page take so long to upload because it tries to download (ga) JavaScript file generated from Google.

Any solution?

like image 961
asalhani Avatar asked Feb 01 '12 07:02

asalhani


1 Answers

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.

like image 56
autonomatt Avatar answered Dec 22 '22 08:12

autonomatt