2016-10-12 121 views
-1

我必须为我的国家的IP列表文件,因为像这样(txt文件):IP检查通过列表

45.123.116.0/22 
5.2.80.0/21 
5.11.128.0/17 
5.23.120.0/21 
5.24.0.0/14 
etc 

我有两个问题。

1-我可以转发用户,如果他通过.htaccess文件在该列表中? (如果他是,使用这个地址..如果不是这个地址)

2-如何通过PHP检查'用户是否在我的国家'?我的意思是,我怎么能说出那样的话..

if (strstr('list.txt',$_SERVER['REMOTE_ADDR'])) 
+0

你不能用'.htaccess'来做这个。你需要使用php来检查它,然后重定向到新地址。 – Mohammad

回答

0

* 1)的.htaccess文件

访客阻止由Apache Web服务器提供的设施使我们能够拒绝对特定访问者,或允许访问特定的访问者。这是阻止不受欢迎的来访者,或者只允许对网站的某些部分网站的所有者获得非常有用,如管理*

ErrorDocument 403 /specific_page.html 
area.* 
order allow,deny 
deny from 255.0.0.0 
deny from 123.45.6. 
allow from all 

当使用“秩序允许,拒绝”指令的要求必须匹配“允许”或“拒绝”,否则请求将被拒绝。

文档1)http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order

DOC 2)http://www.htaccess-guide.com/deny-visitors-by-ip-address/

2)概念(不能说这个原样工程....)的证明

$current_ip = $_SERVER['REMOTE_ADDR']; 

$valid_ip = false; 

// Convert IPs to Regex 
foreach($cfg['ipallowed'] as $index=>$ip); 
{ 
    $ip = str_replace('.', '\\.', $ip); 
    $ip = str_replace('*', '[0-9]|^1?\\d\\d$|2[0-4]\\d|25[0-5]'); 
    if (preg_match($ip, $current_ip) 
    { 
     $valid_up = true; 
     break; 
    } 
} 

if ($valid_ip) 
0

1;你可以通过IP重定向到一个页面:

# Redirect a user to /specific_page.html based on their IP address. 
RewriteCond %{REMOTE_ADDR} ^10\.0\.0\.2$ [OR] 
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.2$ 
RewriteCond %{REQUEST_URI} !specific_page\.html$ 
RewriteCond %{REQUEST_URI} !\.(js|png|gif|jpg|css)$ 
RewriteRule^/specific_page.html [R=302,L] 

2;见this question/answer其中建议使用http://www.hostip.info/use.html

+0

它适用于静态IP,并且不能在文件中使用IP列表。 – Mohammad

+0

对不起,没有看到。如果文件中有一个列表,你必须在你的global/header/bootstrap文件中使用PHP来检查和重定向。如果你有一个列表,为什么不把这个列表放在htaccess中呢? – Egg

+0

你能告诉我如何从文件读取ip列表并插入到.htaccess中? – Mohammad