- 论坛徽章:
- 0
|
想做一个ip管理程序,不知道如何实现
- // -- 获取客户端IP
- function get_client_ip() {
- if (!empty($_SERVER["HTTP_CLIENT_IP"])) return $_SERVER["HTTP_CLIENT_IP"];
- elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) return $_SERVER["HTTP_X_FORWARDED_FOR"];
- else return $_SERVER["REMOTE_ADDR"];
- }
- // -- 判断是否为允许的IP段
- function detect_allowed_ip($ip_list) {
- $ip_list = explode(",", $ip_list);
- $client_ip_list = explode(".", $client_ip);
- for ($i=0; $i<count($ip_list); $i++) {
- $ip_list[$i] = trim($ip_list[$i]);
- $temp_list = explode(".", $ip_list[$i]);
- $pass_temp = TRUE;
- for ($j=0; $j<count($temp_list); $j++) {
- if ($temp_list != "*") {
- if ($client_ip_list[$j] != $temp_list[$j]) {
- $pass_temp = FALSE;
- break;
- }
- }
- }
- if ($pass_temp) return TRUE;
- }
- return FALSE;
- }
复制代码
示例:
- // -- 允许访问的IP地址列表
- $allowed_ips = "127.0.0.1, 192.168.0.*, 10.21.*.*, 61.*";
- // -- 拒绝其他IP访问该页
- if (!detect_allowed_ip($allow_ips)) die("对不起,你无权访问本页!");
复制代码 |
|