Jump to content

Sql Injection Protection


wright67uk

Recommended Posts

Would you say that this code is safe from SQL injection?

If not, then why?

 

<?php
if ($_SERVER['REQUEST_METHOD']=='POST'){
$con = mysql_connect( "###","###","###" );
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("###", $con);
$n = mysql_real_escape_string($_POST['name']);
$e = mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO waiting (name, email) VALUES('$n','$e')";
if (!mysql_query($sql,$con)){ die('Error: ' . mysql_error()); }
echo "Thankyou! We will be in touch soon.";
mysql_close($con);
}
?>


<?php if ($_SERVER['REQUEST_METHOD']<>'POST'){ ?>
<p> This website is under construction!<br />
Enter your details and we will get back to you as soon as we are all done.<br />
<br />
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
Name: <input type="text" name="name" /><br /><br />
Email: <input type="email" name="email" />
<br /><br />
<input type="submit" value="Submit" />
</form></p>

<?php } ?>

Link to comment
Share on other sites

Yes, your code is safe against SQL injections. There are only two variables used in the query, and you've correctly escaped them with mysql_real_escape_string ().

What your code isn't protected against, however, is HTML injections (XSS etc). To protect yourself against that, please see this article. You'll also want to have a look at rawurlescape () and htmlspecialchars ().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.