| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| SI: ServiceInteract | 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. |
| |
| | #3 (permalink) | |
| SI: ServiceInteract | Quote:
Just wondering what are the various options, Thanks, AzN | |
| |
| | #5 (permalink) |
| Senior Member | 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 | 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 |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |