Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

best way to sanitize user input for PDO queries

Hi,

im curious what is the best way to sanitize user inputs when running PDO sql queries

im used to using mysqli procedural style where i just escaped all my variables and used only self created variables. any advice is greatly appreciated
0
J N
Asked:
J N
  • 3
  • 3
  • 2
4 Solutions
 
GaryCommented:
You don't need to sanitize anything on insert.
Maybe someone adds some HTML etc to their input and you want to remove it then you could use regex to remove it.

preg_replace('/<[^>]*>/', '', $input)

But only if it shouldn't be there.

You could just leave everything as is and then escape anything untoward when you send it to the browser.
0
 
J NUnicorn wranglerAuthor Commented:
I actually like having the ' " if the user would like to write i'll so id i do not have to user regex im liking the new technology. however as i was aware of the security vulnerabilities with mysqli im not with pdo so im trying to do some research


when i sent the variables to the prepared statement in the SQL server i dont need to clean them? and if so how would i go about it

for example could i just send $_POST['user_input']
0
 
GaryCommented:
Not sure what you mean I actually like having the ' " if the user...
It doesn't remove apostrophes etc if that's what you mean, for example say someone entered this for a name input
<b>John</b>
The regex above would remove the BOLD tags (or any html tags) so you would end up inserting into the db just
John

The basic premise is
$stmt = $conn->prepare('insert into mytable (mycol) values (:myval)');
$stmt->execute(array( ':myval' => $_POST['user_input']  ));

Open in new window

You don't need to do anything else to the variables, they cannot do anything nefarious to your database or screw up your sql statement.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
All external data is by definition tainted.  Start with that premise, then begin to deconstruct it into things that are appropriate for your applications.

Do you expect a number in a request variable?  Check to see if it is a number.  Are you looking for a person's name?  See if it has a reasonable length and a reasonable character set  Do you expect a ZIP code?  It needs to be five integers or nine integers, but some people will write 22101-3104 so a reasonable test might be to eliminate the non-integer characters then test the length for five or nine.  Do you expect a USA telephone number?  Make sure the second digit it not 9.  Etc.  Are you looking for input from a human instead of a 'bot?  Check the CAPTCHA.

There is no one formula for everything, just common sense.  Dealing with quote marks like in the name O'Brien is just the very tip of the iceberg.
0
 
J NUnicorn wranglerAuthor Commented:
I GOTCHA !!!

i dont have to do anything special other than testing the data is what i want as the statement is already send im just filling in the blanks

is this correct?
0
 
Ray PaseurCommented:
Sidebar note about HTML when it's not expected... PHP has a function for that.
http://php.net/manual/en/function.strip-tags.php

But what would you have the script do if it detected that someone was putting in malicious JavaScript?  Would you store it or reject it?  When you echo any text to the browser, you really, really want to use htmlentities() or similar.  Some further good reading on this sort of thing is available here:
http://php.net/manual/en/security.php
0
 
GaryCommented:
Yep. As Ray says you obviously need to be checking it is the right format for the field.
0
 
J NUnicorn wranglerAuthor Commented:
thanks guys

taking this in baby steps

i dont want to get caught up and then realize later i forgot a huge issue
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now