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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.