Jump to content

php ip log help


phpnoobie9

Recommended Posts

The below logs a users ip to the database. I need help with finding duplicates. If the ip is already in the database I don't want to add the ip again. How would I prevent that...guessing it should go in the else {}. Can I just negate $ipquery in else{}?

$ip = $_SERVER['REMOTE_ADDR'];

$ipduplicate = mysql_query("SELECT * FROM ip WHERE ip==$ip LIMIT 1");

if ($ip !== $ipduplicate) {
$ipquery = mysql_query("INSERT INTO ip (ip) VALUES (\"$ip\")");
} else {
do this is duplicate
}

Link to comment
https://forums.phpfreaks.com/topic/97831-php-ip-log-help/
Share on other sites

You don't use double equal signs in a query.

 

<?php

$ip = $_SERVER['REMOTE_ADDR'];

$ipduplicate = mysql_query("SELECT COUNT(*) FROM ip WHERE ip='$ip'");
$count = mysql_result($ipduplicate, 0);

if ($count <= 0) {
   $ipquery = mysql_query("INSERT INTO ip (ip) VALUES ('$ip')");
} else {
   echo "ERROR: Duplicate";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/97831-php-ip-log-help/#findComment-500529
Share on other sites

Make the IP a unique field. Then handle the error that it would throw when trying to add it.

 

OR

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];

$result = mysql_query("SELECT DISTINCT * FROM `ip`");
if (mysql_num_rows($result) > 0)
{
$ipquery = mysql_query("INSERT INTO `ip` (`ip`) VALUES ('$ip')");
}
else
{
// the ipquery will not have run.
}
?>

Link to comment
https://forums.phpfreaks.com/topic/97831-php-ip-log-help/#findComment-500531
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.