Log in

View Full Version : How to: Prevent Duplicate Accounts by IP address?



lucilled
07-07-2010, 01:14 AM
How do I create a code in PHP and MySQL to prevent people from creating duplicate accounts by recording their IPs in the database?

So far I've found this piece of PHP code but not sure what to do for the database:



<?php

// * Check for duplicates.

$request = "SELECT * FROM jamroom_check_dup_ip";
$db_result = mysql_query($request);
$ipaddress = mysql_fetch_object($db_result);

$num_rows = mysql_num_rows($db_result);
$row_count = 0;

while ($row_count < $num_rows) { // While I haven't checked all the rows.
$ipaddress = mysql_fetch_object($db_result); // Put each object into $ipaddress.
$row_count++; // * And add one to row count.
if ($ipaddress->session == $session) { // If session equates to session...
if ($ipaddress->username == $username) {
if ($ipaddress->password == $password) {
if ($ipaddress->day == $day) {
if ($ipaddress->securityquestion == $securityquestion) {
if ($ipaddress->securityanswer == $securityanswer) {
displayError("Duplicate accounts are not allowed.");
exit();
}
}
}
}
}
?>



Any help is greatly appreciated.

Thanks in advance.

katierosy
07-08-2010, 12:50 PM
if "jamroom_check_dup_ip" is the table where ip addresses are stored
you may go like this

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

$sql="select ipfieldname from jamroom_check_dup_ip where ipfieldname='$ipnow'";

$rs = mysql_query($sql);
$num = mysql_num_rows($rs);
if($num<1){
// add the records
} else {
print 'Duplicate IP or record from same Ip already exists in the database';
}
?>