2016-11-10 92 views
2

如果我的請求似乎很傻,我很抱歉。 我已經找了很長時間,但沒有運氣。 基本上我想要做的是:我想當訪問者訪問我的link'http://mysite。 com/redirect .php',我的php腳本獲取他的IP地址,檢查它是否存在於存儲在文件'hits.txt'中的ips數組中,如果它確實將其重定向到另一個頁面say'google.com' 如果不存在,則將其IP地址存儲在該文件中,然後將其重定向到另一頁,稱爲「yahoo.com」。 等等,等他回來再次訪問時,他會被重定向到google.com。 ofcourse我最終的目的是做一個獨特的ip訪問腳本。 如果你有一個想法如何做到這一點沒有數據庫和SQL我會很感激,如果你認爲它只能用SQL來完成,那麼請建議我最簡單的方法。 到目前爲止我的代碼,但它不工作:獨特的IP訪問重定向在PHP中沒有mysql

<?php 
// Unique Hits PHP Script 
// ----------- March 2004 
// Contact author: [email protected] 

$log = 'hits.txt'; 

$IP = getenv (REMOTE_ADDR); 
$add = true; 
$hits = 0; 

if (!file_exists ($log)) { 
    echo "Error: $log does not exist."; 
    exit; 
} 

$h = fopen ($log, 'r'); 



    if (in_array($IP, array($h))){ 


     header("Location: http://google.com"); 
    } 
     else{ 
     $fp = fopen('hits.txt', 'a'); 
     fwrite($fp, "'"); 
fwrite($fp, $IP); 
fwrite($fp, "'"); 
fwrite($fp, ','); 
fclose($fp); 


     header("Location: http://yahoo.com"); 

     } 





fclose($h); 
?> 

讚賞並感謝你們。

+0

http://pastebin.com/adc6ucZg –

+3

你_do_知道,也沒有明確的訪問者與某個IP地址之間的關聯?那麼基於一個不成立的假設來實現邏輯有什麼意義?從您的角度來看,許多訪問者可能會共享相同的IP地址。單個訪問者即使在同一時間也可能使用不同的IP地址。 – arkascha

+1

使用數據庫,這將變得非常緩慢,隨着文件的增長 – 2016-11-10 08:25:07

回答

1

謝謝你,你幫助了非常多,餅乾的工作非常出色,大加讚賞。 餅乾+ PHP :)

這裏的代碼櫃面別人會需要它:

<!DOCTYPE html> 
 

 
<html> 
 
<body> 
 

 
<?php 
 
$IP = getenv (REMOTE_ADDR); 
 
$verifier = "verify"; 
 
$cookie_value = $IP; 
 
if(!isset($_COOKIE[$verifier])) { 
 
    setcookie($verifier, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day 
 
    header("Location: http://google.com"); 
 
} else { 
 
    header("Location: http://yahoo.com"); 
 
} 
 
?> 
 

 
</body> 
 
</html>