Reputation: 735
I am writing a thumb up/down rating system on items, The rating system should allow the user to rate only one time. I have an SQL table in the database named rating with columns : id, itemname, commentno. and another table ip_check that has : id, commentno, ip.
The commentno will be a foreign key and the syntax to get the ip should be like this : <?php $ip = $_SERVER['REMOTE_ADDR']; ?>
as I checked on line on several links like this one.
Now what is the proper and easiest way to add a mysql if condition. I checked this link as well but I am still looking for something more specific, any help on this?
Upvotes: 3
Views: 312
Reputation: 134
For saving and in Mysql I recommended use Mysql function INET_NTOA() and INET_ATON()
For validating and get IP address I using this functions:
function is_ip($ip) {
global $localIPs;
$test = 0;
$pattern='/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/';
$ip = explode(',', $ip);
$ip = array_map('trim', $ip);
foreach ($ip as $key => $value) {
if(array_search($value, $localIPs)===false && preg_match($pattern, $value)) $test = $value;
}
if(!empty($test)) return $test;
else return false;
}
function get_ip() {
$ip='';
if(!empty($_SERVER['HTTP_X_REAL_IP']) && is_ip($_SERVER['HTTP_X_REAL_IP'])) $ip=is_ip($_SERVER['HTTP_X_REAL_IP']);
elseif(!empty($_SERVER['HTTP_CLIENT_IP']) && is_ip($_SERVER['HTTP_CLIENT_IP'])) $ip=is_ip($_SERVER['HTTP_CLIENT_IP']);
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && is_ip($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip=is_ip($_SERVER['HTTP_X_FORWARDED_FOR']);
else $ip=is_ip($_SERVER['REMOTE_ADDR']);
return $ip;
}
Upvotes: 3