0

I'm using MySQL myisam and some php code to prevent the creation of duplicate ip in popupip table

$userip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$date = date('Y-m-d');
$result = mysql_query("SELECT `ip` FROM  `popupip` where ip = '$userip' AND userid = $secid AND date='$date'");
$num = mysql_num_rows($result);
    if ($num > 0) {
    // **it is duplicate Do not insert it in popupip table**
    }else{
    // **it is not duplicate ip insert it in popupip table**
    }

above code is an example.I know full code.

but when I look to my phpmyadmin popupip table there is a few duplicate ip for user (the exact same ip address for the user on the same date)

how it is possible?

extra information: in popupip userid is int(11) , date is "date type like 2014-05-30" and ip is varchar. this page may opens "as fast as possible at the same time" by popup pages. is there a relation between openning a page fast at same time by a user and duplicate ip creation? is there a bug in MySQL? (maybe a big bug!!!!)

4 Answers4

1

Here's an overview of what's gonna be.

Note: I'm not saying this is the best way, but I'll try to help.

First, Delete all your data in popupip so that would remove all your duplicates.

Then to start of, get the client's ip. $client_ip = get_ip();

function get_ip() {

    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'Unknown IP';
    return $ipaddress;
}

so the $client_ip has the value of the user's IP. (obviously)

So to determine if the user has already the IP in the database.

$query = $this->db->prepare("SELECT ip_address FROM accounts WHERE user_id = :user_id AND client_ip = :client_ip");
$query->execute(array(':user_id' => $user_id, ':client_ip' => $client_ip));


// No IP in the table
if ($query->rowCount() == 0) {
    //Put INSERT SQL here.
}
Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
0

you should try using DISTINCT to avoid duplicates in select query,

SELECT DISTINCT `ip` FROM  `popupip` WHERE ip = '$userip' 
AND userid = $secid 
AND DATE='$tarikh'
ravikumar
  • 893
  • 1
  • 8
  • 12
0

You should add UNIQUE index to the table. Here is sytnax:

ALTER TABLE yourTableNAme ADD UNIQUE (
`yourColumnName`
)
usef_ksa
  • 1,669
  • 3
  • 23
  • 40
0

As in your code ,I think you first check for an ip that already exist in the table for prevent duplication using this query

SELECT ip FROM popupip where ip = '$userip' AND userid = $secid AND date='$tarikh';

But this query will fail if two userid(users) comes from same ip adress

For eg:

  1. SELECTipFROMpopupip` where ip = '127.0.0.1' AND userid = 1 AND date='26-05-2014';

    returns 0 rows and inserted ip.

  2. SELECT ip FROM popupip where ip = '127.0.0.1' AND userid = 2 AND date='26-05-2014';

    also returns 0 rows and inserted ip.

    So first check ip only,{without any AND or OR} like

    SELECT ip FROM popupip where ip = '$userip';

    If this query returns 0 insert new row else avoid insertion.

    Hope this helps.

Nidhin
  • 1,818
  • 22
  • 23