在php中,如果我有一个成员系统,我如何限制某些IP地址只能以管理员/版主身份登录?我知道有
$_SERVER["REMOTE_ADDR"]
但这包括使用代理或路由器的人吗?
还是有其他方法吗?
由于
答案 0 :(得分:0)
你可以尝试
if ($_SERVER['REMOTE_ADDR'] == 'xxx.xxx.x.xxx') {
//Allow login as administrator
}
但遗憾的是,$ _SERVER [' REMOTE_ADDR']是从客户端发送的,很容易被欺骗(请参阅How to get the client IP address in PHP?)。
最好的办法是使用上述解决方案和强大,安全的管理员密码的组合,因此,如果IP被欺骗,黑客仍然需要猜出强密码!
答案 1 :(得分:0)
您可以检查IP范围,以查看用户是否从右侧Intranet连接:
/**
* Check if a given ip is in a network
* @param string $ip IP to check in IPV4 format eg. 127.0.0.1
* @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed
* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
// $range is in IP/CIDR format eg 127.0.0.1/24
list( $range, $netmask ) = explode( '/', $range, 2 );
$range_decimal = ip2long( $range );
$ip_decimal = ip2long( $ip );
$wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1;
$netmask_decimal = ~ $wildcard_decimal;
return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
}