Logging IP in MYSQL Database

Help! I don’t know why this isn’t working. I’m trying to log an ip in a database and retrieve it along with a integer. All it has been doing is logging 0 as the ip and 0 as the integer.

[php] $data = mysql_query(“SELECT ip,points FROM information”)
or die(mysql_error());

if (isset($_SERVER[“HTTP_X_FORWARDED_FOR”])) {
$ip = $_SERVER[“HTTP_X_FORWARDED_FOR”];
} else {
$ip = $_SERVER[“REMOTE_ADDR”];
}

while($info = mysql_fetch_array( $data ))
{
if($info[‘ip’] != null && $info[‘ip’] == ‘ip2long($ip)’){
Print “You have $info[points] points!”;
break;
}
if($info[‘ip’] != null && $info[‘ip’] != ‘ip2long($ip)’){
mysql_query(“INSERT INTO information VALUES (‘INET_ATON($ip)’,‘0’)”);
Print “You have 0 points!”;
break;
}
}[/php]

if (isset($_SERVER[“HTTP_X_FORWARDED_FOR”])) { will always be truthy as that specific variable is always set. Try using !empty() instead of isset() and let me know if it works.

It still does not work. Nothing has changed. When I do
Print $ip;

It prints out this.
::1

IPv6.

Use the MySQL INET6_ATON for it.

So would this work?

[php] $data = mysql_query(“SELECT ip,points FROM information”)
or die(mysql_error());

if (!empty($_SERVER[“HTTP_X_FORWARDED_FOR”])) {
$ip = $_SERVER[“HTTP_X_FORWARDED_FOR”];
} else {
$ip = $_SERVER[“REMOTE_ADDR”];
}

while($info = mysql_fetch_array( $data ))
{
if($info[‘ip’] != null && $info[‘ip’] == ip2long($ip)){
Print “You have $info[points] points!”;
break;
}
if($info[‘ip’] != null && $info[‘ip’] != ip2long($ip)){
mysql_query(“INSERT INTO information VALUES (‘INET6_ATON($ip)’,‘0’)”);
Print “You have 0 points!”;
break;
}
}[/php]

Edit: It still doesn’t work. Im currently using the code above. Also when I refresh the page it just sends multiple queries to MYSQL.

Hmm now Im just having trouble logging the IP. I keep getting 0 in MYSQL.

Sponsor our Newsletter | Privacy Policy | Terms of Service