Created
July 25, 2017 11:01
-
-
Save calexandrepcjr/959b15a9c696e97a8b2e16cd926e985b to your computer and use it in GitHub Desktop.
Quick idea of blocking external access with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MY_Controller | |
{ | |
private function ipVersion($ip) | |
{ | |
return strpos($ip, ':') === false ? 4 : 6; | |
} | |
public function isLocal() | |
{ | |
$userIP = $_SERVER['REMOTE_ADDR']; | |
$serverIP = $_SERVER['SERVER_ADDR']; | |
if ($this->ipVersion($userIP) == 4) { | |
if ($userIP != '127.0.0.1' && $userIP != 'localhost'){ | |
$userIP = explode('.', $userIP); | |
$serverIP = explode('.', $serverIP); | |
if ($userIP[0] == $serverIP[0] && $userIP[1] == $serverIP[1]){ | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
return true; | |
} | |
} else { | |
$checkUserIPv6 = strpos($userIP, 'fe80:') === false; | |
if ($userIP != '::1' && $userIP != 'localhost' && $checkUserIPv6){ | |
if ($userIP[0] == $serverIP[0] && $userIP[1] == $serverIP[1]){ | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
return true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment