2013-07-16 77 views
0

我使用以下方法在表單提交期間捕獲訪客IP地址。將IP與SQL表值進行比較

$users_ip=$_SERVER['REMOTE_ADDR']; 

我現在想要做的是看看這個IP變量提交評論時之前被使用,任何人都可以點我在正確的方向?

也許一個like SQL命令?

+0

您是否已經存儲的IP地址時,他們提交評論? – andrewsi

+1

注意事項以防萬一您不知道:這不可能是非常一致的,因爲大多數用戶的IP地址將會頻繁更改。 –

+0

你能否提供存儲IP的表格模式? – DevlshOne

回答

1

假設你在指定的表存儲客戶端IPS:「IPS」,然後使用此:

$connection = mysql_connect($your_db_host, $your_user_account, $your_password); 
$mysql_select_db($your_db_name); 

$ip = $_SERVER['REMOTE_ADDR']; 
$sql = "select 1 from `ips` where `ip`='$ip'"; 
$sql_resource = mysql_query($sql); 
$mysql_close($connection); 

if($sql_resource && mysql_num_rows($sql_resource) > 0){ 
    // your logic code if the ip existed in the db 
    echo 'The ip has been used before'; 
} else { 
    // code if the ip not existed in the db 
    echo 'The ip has not been used before yet'; 
} 
+0

謝謝你,不錯的例程:) – fightstarr20

1

有一個很好的教程解釋如何將IP地址存儲在MySQL。總之,並將其轉換爲長的很像提出在this comment,然後用簡單的語句SELECT找到它:

"SELECT COUNT(*) FROM comment WHERE ip = " . ip2long($_SERVER['REMOTE_ADDR'])