Jump to content

Stopping duplicate registrations?


Dark57

Recommended Posts

Ok so for some reason my code is allowing duplicate entries to be submitted and even blank entries. I'm not sure why, its like its skipping the entire checking process and going straight to entering it into the database.  I've been fiddling with it for a day or two, trying different things and this is what I have right now, this set of code comes after it checks to see if the input fields are empty.  Can anyone see anything wrong with this?

 

	//If not empty, compare emails to database

$sql = "SELECT COUNT(*) FROM mytable WHERE email = '$email'";
$result = mysql_query($sql);

if($result)
{
    if(mysql_num_rows($result) == 1)
    {
        $count = (int)mysql_result($result, 0, 0);
        if($count > 0)
        {
        	$_SESSION['emailfail']=1;
             header("location:register_fail.php");
        }
    }
}

$sql = "SELECT COUNT(*) FROM mytable WHERE username = '$username'";
$result = mysql_query($sql);

if($result)
{
	if(mysql_num_rows($result) == 1)
    {
        $count = (int)mysql_result($result, 0, 0);
        if($count > 0)
        {
        	$_SESSION['usernamefail']=1;
             header("location:register_fail.php");
        }
    }
}

else
{
//If emails arent duplicates, write to file.
// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$email = stripslashes($email);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$password = mysql_real_escape_string($email);
$password = md5($password);
$register="INSERT INTO $tbl_name (username, password, email)
VALUES
('$username','$password','$email')";

if (!mysql_query($register,$con))
  {
  die('Error: ' . mysql_error());
  }
header("location:register_success.php");
}

Link to comment
https://forums.phpfreaks.com/topic/198442-stopping-duplicate-registrations/
Share on other sites

You need to call exit after any calls to header to prevent the script from continuing to execute.

 

[ot]

This piece of logic might also want looking at.

$password = mysql_real_escape_string($password);
$password = mysql_real_escape_string($email);
$password = md5($password);

[/ot]

Also, your else{} clause that contains the INSERT query will be executed when the query that is testing the username fails due to an error in the query (i.e. $result is false), so I would guess that there is some problem with your database server or the query has a problem with the table or column name.

 

Both of the if($result) tests should have an else{} clause that handles your error reporting for when the queries fail. For debugging purposes, you could echo mysql_error() in the else{} code to determine why the query is failing.

Psuedo code...

 

$t_user = sanitized user name from form

$t_emal = sanitized email from form

 

querys = select * from table where username = '$t_user' OR email = '$t_email'

 

$num_records = count of querys results

 

if $num_records >0  send back to form page

 

else enter data in table and move to login success page

 

<?PHP
session_start(); 

/*
check to see if form submitted
check to see if form data is present
get and sanitize the form data
connect to database
*/

$_SESSION['emailfail'] = 0;
$_SESSION['usenamefail'] = 0;

$sql = "SELECT * FROM mytable WHERE email = '$email'";
$result = mysql_query($sql);
$number=mysql_num_rows($result); 

if($number>0) {
$_SESSION['emailfail']=1;
}

$sql = "SELECT * FROM mytable WHERE username = '$usename'";
$result = mysql_query($sql);
$number=mysql_num_rows($result); 
if($number>0) {
$_SESSION['usernamefail']=1;
}

if($_SESSION['emailfail']>0 OR $_SESSION['usernamefail']>0} {
?>
<meta http-equiv="Refresh" content="0;url=register_fail.php">
<?PHP
exit();
}
/*
process and insert data into data base here then...
*/
?>
<meta http-equiv="Refresh" content="0;url=register_success.php">
<?PHP
exit();
?>

Dark...

 

this portion

/*
check to see if form submitted
check to see if form data is present
get and sanitize the form data
connect to database*/

 

was meant for him to acutually do. He was to replace that comment with the actual code for  doing so.

 

When that is accomplished, then $email and $username will contain the values from his form page. Therefore the queries WILL check to see if either the username and/or the email are already in the database

 

(make sens or am I missing something)

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.