[英]Redirect if User's IP Address is in .txt file
我已經嘗試了REMOTE_ADDR
以及HTTP_X_FORWARDED_FOR
但我仍然遇到問題, HTTP_X_FORWARDED_FOR
工作。
基本上,如果在文本文件中找到用戶IP,我希望用戶被重定向到cnn.com
,如果沒有IP,則重定向到google.com
以下是文件中不使用我的IP的代碼:
<?php
$iplist = @file("ip.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$iparray = in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $iplist);
//$iparray = in_array($_SERVER["REMOTE_ADDR"], $iplist);
//###if ?test= is not empty
if (!empty($_GET["test"])){
if($iparray){
header("Location: http://cnn.com");die;
}
}
header("Location: http://google.com");
?>
注意:測試時,我的URL末尾有?test = 1
我想保持if語句的格式+停止執行其余代碼,如果可能的話,那么header("Location: http://google.com");
不執行。
如果您的IP在文件中且文件路徑正確,請嘗試以下操作:
if (false !== strpos(file_get_contents($filename), $ip_to_search)){
// ip in the file
}
else {
// otherwise
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.