| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| is on hiatus Join Date: May 2006
Posts: 2,453
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | PHP: Best way to sanitize inputs Been reading about mysql injections, so I'm wondering what's the best way to sanitize inputs to protect against injection in PHP? I've read at a place converting strings into hex or base64 encoded strings can be a good alternative. Thanks.
__________________ Currently on hiatus. Back whenever. |
| |
| | #2 (permalink) |
| Senior Member Join Date: Aug 2005 Location: East Yorkshire, England
Posts: 2,689
![]() ![]() ![]() ![]() ![]() ![]() ![]() | This is the best way: PHP Code: |
| |
| | THREAD STARTER #3 (permalink) | ||||
| is on hiatus Join Date: May 2006
Posts: 2,453
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Just wondering what are the various options, Thanks, AzN
__________________ Currently on hiatus. Back whenever. | ||||
| |
| | #5 (permalink) |
| Senior Member Join Date: Jul 2005 Location: NJ
Posts: 1,219
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | this is what i use: PHP Code:
__________________ Hacksar.com - Your source for random computer tips and tricks! MySiteMemberships.com - Keep track of your site registration information! Like my post? Rep is appreciated! |
| |
| | #7 (permalink) |
| NamePros Member Join Date: Sep 2006
Posts: 99
![]() ![]() | The best way to prevent injection is to hack yourself (or at least think about it). If you're expecting numerical input, don't could on a user to make it numerical. Take a look at the following snippet: PHP Code: What happens if I enter '1' for start and '2 union Select name, password from players' for end? I get the username and password for every entry in the table! What did we do wrong here? 1. Obviously, we didn't use single quotes inside the mysql query. If you use single quotes surrounding every variable AND use mysql_real_escape_string, you're much safer. 2. We trusted user input. We didn't check if both $start and $end were numerical. We could have even been lazy and done $start = floor($start). You'd be better off using is_int($start) at least is_numeric($start) 3. The code is lazy. It doesn't check if $Query actually worked, which can throw a mysql_fetch_array warning if there are no results. But this is because I was lazy writing my example, not trying to make a point. So the whole point of this was to show you there isn't one fool-proof way to stop mysql injection. mysql_real_escape_string does not work every time. You need to use your brain and understand how injection really works before you can defend against it. Bruce |
| |