Created
April 7, 2021 22:07
-
-
Save alejoasotelo/f4a68224e9c14f3b48d7f980c3fb3059 to your computer and use it in GitHub Desktop.
Debuggear según IP
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 MyIP { | |
private static $ips = []; | |
/** | |
* Setea las IPs del cliente | |
* | |
* @return bool | |
*/ | |
public static function setMyIps($ips) { | |
if (is_string($ips)) { | |
$ips = [$ips]; | |
} | |
self::$ips = $ips; | |
} | |
/** | |
* Devuelve true o false si la IP del cliente | |
* es la IP previamente configurada en self::setMyIPs($ips); | |
* | |
* @return bool | |
*/ | |
public static function isMyIP() { | |
$clientIP = self::getClientIP(); | |
return in_array($clientIP, self::$ips); | |
} | |
/** | |
* Devuelve la IP del cliente. | |
* | |
* @return string IP | |
*/ | |
public static function getClientIP() { | |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} else { | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
return $ip; | |
} | |
} | |
?> | |
/** | |
* Ejemplo de uso: | |
**/ | |
<?php | |
$myIps = ['192.168.0.1', '156.231.564.87']; | |
MyIP::setMyIPs($myIps); | |
if (MyIP::isMyIP()) { | |
echo 'Estoy debuggeando solo si mi IP está dentro de la lista sino no pasa nada y sigue corriendo todo normalmente.'; | |
echo 'Esto serviría para debuggear en producción (no recomendable, pero puede pasar). | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment