Reputation: 70
This is an issue that's left me scratching my head. Most of the time, it returns the IP address fine, but on occasion it logs it as "0" which causes me issues as I am using this for voting. As you can see in the code below, I switch it over into long form and use the conditional if ip value == 0 then allow to vote. I have it double protected with 17 hour cookies, but I'd like to solve this issue if I can just to be neat and tidy. Plus I think there are a few smart folks who clear their cookies and vote multiple times. Here's the code I'm using:
$ip = $_SERVER['REMOTE_ADDR'];
$iplong = ip2long($ip);
$fetch = mysql_query("SELECT * FROM ip WHERE ipaddress='$iplong' AND
url='$urlupdate'");
if(mysql_num_rows($fetch) == 0) {
$query = mysql_query("INSERT INTO ip (url,ipaddress) VALUES ('$urlupdate','$iplong')");
}
$update = "UPDATE pages SET counter = counter +1 WHERE url = '$urlupdate'";
$result = mysql_query($update);
Like I said, most of the time this works like a charm, and the cron job clears it up once a day with no issues. At first I thought it was a noscript issue, but it's triggered through an onclick. Meaning that code never gets parsed, values don't get inserted, and essentially the link just takes them back to the results page without ever logging info. I'm so stumped as to what could be causing this!
Upvotes: 2
Views: 1032
Reputation: 360562
Using ip2long in 32bit PHP installs is tricky. For simple MySQL operations, you're better off passing the IP around as a string and using MySQL's native inet_ntoa/inet_aton functions to convert for storage.
$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$sql = "INSERT INTO ... VALUES (INET_ATON('$ip'));";
and then
$sql = "SELECT INET_NTOA(ipaddress) AS ip ...";
for later retrieval.
Upvotes: 2