Skip to content

Instantly share code, notes, and snippets.

@coquer
Created February 25, 2016 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coquer/cdd9f021ff58f61c4edf to your computer and use it in GitHub Desktop.
Save coquer/cdd9f021ff58f61c4edf to your computer and use it in GitHub Desktop.
Simple way to detect Tor exit node using laravel 5.2 and PHP 7.0
/**
* Detect if current User is using tor network
* @return bool
*/
public static function detectIfTorNetwork(): bool {
return ( gethostbyname(
self::reverseIPOctets( getenv( 'REMOTE_ADDR' ) ) . "." .
getenv( 'SERVER_PORT' ) . "." .
self::reverseIPOctets( getenv( 'SERVER_ADDR' ) ) .
".ip-port.exitlist.torproject.org" ) == "127.0.0.2" ? true : false
);
}
/**
* Reverse the IP
*
* @param string $input
*
* @return string
*/
public static function reverseIPOctets( string $input ) : string {
if ( filter_var( $input, FILTER_VALIDATE_IP ) ) {
$ip = explode( '.', $input );
$ip = array_reverse( $ip );
$ip = implode( '.', $ip );
return $ip;
}
return '127.0.0.1';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment