woodplease Posted September 5, 2010 Share Posted September 5, 2010 i have some code which checks to see if a username and an email is in use. from what i can understand, it uses magic quotes to prevent sql injection. i've heard that magic quotes are not going to be in use in php6, so how can i change it so that it uses real escape string instead? if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } if (!get_magic_quotes_gpc()) { $_POST['email'] = addslashes($_POST['email']); } $emailcheck = $_POST['email']; $check = mysql_query("SELECT email FROM users WHERE email = '$emailcheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); if ($check2 != 0) { die('Sorry, the email '.$_POST['email'].' is already registered to another account.'); } Thanks Link to comment https://forums.phpfreaks.com/topic/212582-getting-rid-of-magic-quotes/ Share on other sites More sharing options...
woodplease Posted September 5, 2010 Author Share Posted September 5, 2010 ok, i think i've managed to take out the magic quotes. could someone tell me if what i've done is sufficient to prevent sql injection? // checks if the email is in use $mail = $_POST['email']; $emailcheck = mysql_real_escape_string($mail); //if (!get_magic_quotes_gpc()) { //$_POST['email'] = addslashes($_POST['email']); //} //$emailcheck = $_POST['email']; $check = mysql_query("SELECT email FROM users WHERE email = '$emailcheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the email '.$emailcheck.' is already registered to another account.'); } Link to comment https://forums.phpfreaks.com/topic/212582-getting-rid-of-magic-quotes/#findComment-1107449 Share on other sites More sharing options...
systemick Posted September 5, 2010 Share Posted September 5, 2010 The mysql_real_escape_string function will prevent sql injection. The only other thing I would add is that you don't need the first 2 lines of code. You could just write $check = mysql_query("SELECT email FROM users WHERE email = '".mysql_real_escape_string($_POST['mail'])."'") Link to comment https://forums.phpfreaks.com/topic/212582-getting-rid-of-magic-quotes/#findComment-1107506 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.