0

I sanitise the data I receive from the form in the following way:

$gender = filter_var($_POST['gender'], FILTER_SANITIZE_STRING);
$firstName = filter_var($_POST['firstName'], FILTER_SANITIZE_STRING);
$lastName = filter_var($_POST['lastName'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
$address = filter_var($_POST['address'], FILTER_SANITIZE_STRING);
$numBrochures = (int) filter_var($_POST['quantity'], FILTER_SANITIZE_NUMBER_INT);

The relevant SQL queries that insert the data are as follows:

if (mysqli_query($conn, "INSERT INTO users(firstName, lastName, email, gender) VALUES('$firstName', '$lastName', '$email', '$gender')") == TRUE) {
  logSuccess($file, "Adding user");
}
else {
  logError($file, "Adding user", mysqli_error($conn));
}

$userId = $conn->query("SELECT `userId` FROM users WHERE `firstName` = '$firstName' AND `lastName` = '$lastName' AND `email` = '$email'")->fetch_object()->userId;
if ($userId == false) {
  logError($file, "Fetching user id", mysqli_error($conn));

}

if (mysqli_query($conn, "INSERT INTO brochureOrders(userId, address, numBrochures, message) VALUES('$userId', '$address', '$numBrochures', '$message')") == TRUE) {
  logSuccess($file, "Brochure Order");
  $sendConfirmationEmail = true;
}
else {
  logError($file, "Brochure Order", mysqli_error($conn));
}

However, in my database, I see entries like the following:

address = "vz8y8E  gghwptvvzuak, [url=http://ytvsmximkjnp.com/]ytvsmximkjnp[/url], [link=http://hiabgyvsjifp.com/]hiabgyvsjifp[/link], http://tyvylndqitoy.com/"

Shouldn't the following have taken care of this?

$address = filter_var($_POST['address'], FILTER_SANITIZE_STRING);

Could someone tell me what I am doing incorrectly here?

9
  • This makes me cry... why not use prepared statements :/ Commented Dec 9, 2014 at 18:32
  • filter_var with the 'FILTER_SANITIZE_STRING' filter just strips tags and (optionally) strips or encodes special characters. What were you hoping that it would do? Commented Dec 9, 2014 at 18:33
  • @GeorgeCummins: I was hoping for it to remove URLs. Commented Dec 9, 2014 at 18:34
  • It doesn't do that. As Arian suggests, you should begin using prepared statements via PDO immediately to protect against SQL injection attacks, and write or find a better filtering scheme to remove URLs as needed. Commented Dec 9, 2014 at 18:35
  • @GeorgeCummins: Yes, I will do that. Commented Dec 9, 2014 at 18:36

1 Answer 1

1

Because the OP stated in the comments he wants to switch to prepared statement, I thought I'd show him an example.

Instead of something like this:

if (mysqli_query($conn, "INSERT INTO users(firstName, lastName, email, gender) VALUES('$firstName', '$lastName', '$email', '$gender')") == TRUE) {
  logSuccess($file, "Adding user");
}
else {
  logError($file, "Adding user", mysqli_error($conn));
}

Do something like this:

$query = "INSERT INTO users (firstName, lastName, email, gender) VALUES(?, ?, ?, ?)";

if($stmt = $mysqli->prepare($query)){
    $stmt->bind_param('ssss', $firstName, $lastName, $email, $gender);
    $stmt->exeucte();
    $stmt->close();
}else die("Failed to prepare!");

and this

$query = "SELECT `userId` FROM users WHERE `firstName` = ? AND `lastName` = ? AND `email` = ?";

if($stmt = $mysqli->prepare($query)){
    $stmt->bind_param('sss', $firstName, $lastName, $email);
    $stmt->execute();
    $stmt->bind_result($userId);
    $stmt->fetch();
    $stmt->close()
}else die("Failed to prepare!");
Sign up to request clarification or add additional context in comments.

2 Comments

This is a good improvement to prevent SQL injection attacks, but be aware that it doesn't address the core issue as stated in comments: "I was hoping for it to remove URLs."
@GeorgeCummins yeah i know, but the OP stated in the comments he wanted to switch to prepared statements, so I just wanted to show him an example of what it would look like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.