esahp Posted September 28, 2006 Share Posted September 28, 2006 I have a signup form, and the data on it will get inserted into a MySQL database. Now as far as I know I've taken the proper steps in the following code to prevent SQL, javascript, and html source injections. Is there anything further I can do? Or is what I already have done it.I have the signup form action go to another page, and the contents of said page are:[code]<? $firstname = mysql_escape_string(strip_tags($_POST['firstname'])); $lastname = mysql_escape_string(strip_tags($_POST['lastname'])); $email = mysql_escape_string(strip_tags($_POST['email'])); $phonenumber = mysql_escape_string(strip_tags($_POST['phonenumber'])); $homeaddress = mysql_escape_string(strip_tags($_POST['homeaddress'])); $citystate = mysql_escape_string(strip_tags($_POST['citystate'])); $country = mysql_escape_string(strip_tags($_POST['country'])); $domainname = mysql_escape_string(strip_tags($_POST['domainname'])); $username = mysql_escape_string(strip_tags($_POST['username'])); $password1 = mysql_escape_string(strip_tags($_POST['password1'])); $password2 = mysql_escape_string(strip_tags($_POST['password2'])); $rules = mysql_escape_string(strip_tags($_POST['rules'])); $legalinfo = mysql_escape_string(strip_tags($_POST['legalinfo'])); $age = mysql_escape_string(strip_tags($_POST['age'])); $sitedetails = mysql_escape_string(strip_tags($_POST['sitedetails'])); $aboutus = mysql_escape_string(strip_tags($_POST['aboutus'])); if ($firstname == "") { $errors .= "First Name field was left blank.<br />"; } if ($lastname == "") { $errors .= "Last Name field was left blank.<br />"; } if ($email == "") { $errors .= "Email Address field was left blank.<br />"; } if ($phonenumber == "") { $errors .= "Phone Number field was left blank.<br />"; } if ($homeaddress == "") { $errors .= "Home Address Field was left blank.<br />"; } if ($citystate == "") { $errors .= "City&State field was left blank.<br />"; } if ($country == "") { $errors .= "Country field was left blank.<br />"; } if ($domainname == "") { $errors .= "Your Domain field was left blank.<br />"; } if ($username == "") { $errors .= "Desired Username field was left blank.<br />"; } if (($password1 == "") || ($password2 == "") || ($password1 != $password2)) { $errors .= "Password fields were left blank or do not match.<br />"; } if ($rules == "") { $errors .= "You didn't agree to the rules.<br />"; } if ($legalinfo == "") { $errors .= "You didnt agree to the legal information.<br />"; } if ($age == "") { $errors .= "You didnt state you were over the age of 18.<br />"; } if ($sitedetails == "") { $errors .= "Site Details field was left blank.<br />"; } if ($aboutus == "") { $errors .= "About Us field was left blank.<br />"; } if (isset($errors)) { echo $errors; } else { // SQL Query stuff here echo "Works!"; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/ Share on other sites More sharing options...
oracle259 Posted September 28, 2006 Share Posted September 28, 2006 You should also check the magic_quotes_gpc state (ie on or off) you can try something like this also you can tie ur mysql_real_escape_string tests into the function[code]function makesafe($string) {if (!isset($_REQUEST['$string']) || empty($_REQUEST['$string'])) {die ("Unauthorized action"); }if (!get_magic_quotes_gpc()) { $string = addslashes($_POST['$string']);} else { $string = $_POST['$string'];} $string = mysql_real_escape_string($string); return $string}[/code]Remember though, if you are going to use empty $string can't be 0 or it will return empty. Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100039 Share on other sites More sharing options...
esahp Posted September 28, 2006 Author Share Posted September 28, 2006 I was told I didn't need addslashes(); if I had both mysql_escape_string(); and strip_tags(); Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100042 Share on other sites More sharing options...
oracle259 Posted September 28, 2006 Share Posted September 28, 2006 Im still a newbie so may be but doesnt hurt to do it Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100045 Share on other sites More sharing options...
extrovertive Posted September 28, 2006 Share Posted September 28, 2006 if (get_magic_quotes_gpc()) { $string = stripslashes($_POST['$string']);} else { $string = $_POST['$string'];} $string = mysql_real_escape_string($string); return $string Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100046 Share on other sites More sharing options...
esahp Posted September 28, 2006 Author Share Posted September 28, 2006 That still didn't answer my question.First post: "Is there anything further I can do? Or is what I already have done it."Also, quoting my second post "I was told I didn't need addslashes(); if I had both mysql_escape_string(); and strip_tags();" is this true that I don't need addslashes(); if I have both of the following, or do I still need to include it somewhere?And whats the deal with magic_quotes_gpc? Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100047 Share on other sites More sharing options...
kenrbnsn Posted September 28, 2006 Share Posted September 28, 2006 If you use mysql_real_escape_string() you should not use addslashes().Ken Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100058 Share on other sites More sharing options...
esahp Posted September 28, 2006 Author Share Posted September 28, 2006 Thankyou for giving me a straight answer. Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100066 Share on other sites More sharing options...
bob_the _builder Posted September 28, 2006 Share Posted September 28, 2006 Hi,Save yourself some repetitive typing and use a function:[code=php:0]function ValidateInput($value) { if (!get_magic_quotes_gpc()) { $value = mysql_real_escape_string($value); } $value = trim(strip_tags($value)); return $value; }$firstname = ValidateInput($_POST['firstname']);$lastname = ValidateInput($_POST['lastname']);[/code]Bob Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100075 Share on other sites More sharing options...
esahp Posted September 28, 2006 Author Share Posted September 28, 2006 Actually, with the power of ctrl+v it was quite easy :PI noticed you have mysql_real_escape_string(); whereas I have mysql_escape_string(). From the looks of the php manual mysql_real_escape_string(); is better. Is that true? Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100282 Share on other sites More sharing options...
Jenk Posted September 28, 2006 Share Posted September 28, 2006 Don't need to use strip_tags or trim.[code]<?phpfunction magic_clean ($string){ if (get_magic_quotes_gpc()) $string = mysql_real_escape_string($string); return $string;}?>[/code]Only escape for what you need to escape for, no point using strip_tags if all you are doing is inserting to the DB... you may actually want to allow users to use HTML tags.trim is just unecessary.You also need to refine your variable checking:[code]<?phpif (!empty($_GET['variable'])) $variable = magic_clean($_GET['variable']);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100287 Share on other sites More sharing options...
wildteen88 Posted September 28, 2006 Share Posted September 28, 2006 You can change the whole of the following:[code]$firstname = mysql_escape_string(strip_tags($_POST['firstname'])); $lastname = mysql_escape_string(strip_tags($_POST['lastname'])); $email = mysql_escape_string(strip_tags($_POST['email'])); $phonenumber = mysql_escape_string(strip_tags($_POST['phonenumber'])); $homeaddress = mysql_escape_string(strip_tags($_POST['homeaddress'])); $citystate = mysql_escape_string(strip_tags($_POST['citystate'])); $country = mysql_escape_string(strip_tags($_POST['country'])); $domainname = mysql_escape_string(strip_tags($_POST['domainname'])); $username = mysql_escape_string(strip_tags($_POST['username'])); $password1 = mysql_escape_string(strip_tags($_POST['password1'])); $password2 = mysql_escape_string(strip_tags($_POST['password2'])); $rules = mysql_escape_string(strip_tags($_POST['rules'])); $legalinfo = mysql_escape_string(strip_tags($_POST['legalinfo'])); $age = mysql_escape_string(strip_tags($_POST['age'])); $sitedetails = mysql_escape_string(strip_tags($_POST['sitedetails'])); $aboutus = mysql_escape_string(strip_tags($_POST['aboutus']));[/code]Into just the following few lines:[code=php:0]foreach($_POST as $field => $value){ if(isset($_POST[$field]) && !empty($_POST[$field])) { ${$field} = mysql_real_escape_string(strip_tags($value)); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100302 Share on other sites More sharing options...
bob_the _builder Posted September 28, 2006 Share Posted September 28, 2006 Hi,Personally to lower sql injection risk I would introduce bbcode and then striptags, trim just helps to keep everything tidy.Bob Quote Link to comment https://forums.phpfreaks.com/topic/22331-protection-from-sql-injections/#findComment-100479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.