![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: mysql, php, sql injection |
![]() |
![]() | | LinkBack (10) | Thread Tools | Display Modes | ![]() |
| ||||
| Listed as #2 in the Top 10 Web application vulnerabilities for 2007, injection flaws are something about which every developer should be especially cautious to protect their end users. In this article, I'll discuss protecting oneself from MySQL injection attacks using PHP. Specifically, we're going to look at, improve, and create a generalized function for the "Best Practice" query suggested on PHP.net at PHP: mysql_real_escape_string - Manual . Before proceeding, you'll want to go and take a look at that box on the PHP.net site. Done? Great! Then let's get started. So, from the code in the box, we learn how to protect data using the advantages of mysql_real_escape_string independent of the server's Magic Quotes settings. If we take what's written and put it in the form of a function, we would have something like this: PHP Code: Quote:
PHP Code: PHP Code: And, for those out there who are ahead of the game and have moved to PHP 5, but don't understand how PDO can help protect you, here's a PHP 5 equivalent: PHP Code: Now we have our function all setup and ready for usage, let's begin by creating a table and then view some example usage Code: CREATE TABLE `sample_table` ( `row_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` CHAR( 25 ) NOT NULL , `password` CHAR( 40 ) NOT NULL , `date_added` DATE NOT NULL , `auth_level` TINYINT NOT NULL DEFAULT '0' ) Now, let's assume that we have an HTML page that allows a user to be added. The form might look like Code: <h1>Add User</h1>
<form method="post" action="<?php echo $PHP_SELF;?>">
<label for="username">Username</label> <input type="text" name="username" id="username" />
<label for="password">Password</label> <input type="password" name="password" id="password" />
<label for="auth_level"></label> <select name="auth_level" id="auth_level">
<option value="1">Low Level</option>
<option value="2" selected="selected">Med Level</option>
<option value="3">High Level</option>
</select>
<input type="submit" value="Add User" />
</form> PHP Code: PHP Code: Last edited by TeraTask : 06-15-2007 at 06:58 AM. |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.programmerstalk.net/thread722.html | |||
| Posted By | For | Type | Date |
| Content Farmer: MySQL Injection Attack Prevention in PHP Tutorial | This thread | Refback | 09-08-2007 04:52 PM |
| gpc_magic_quotes: Blogs, Photos, Videos and more on Technorati | This thread | Refback | 06-19-2007 06:10 AM |
| Passing a variable from a form to a MySql query | This thread | Refback | 06-17-2007 10:36 AM |
| Digg / News / Upcoming | This thread | Refback | 06-15-2007 01:08 PM |
| Stumble exchange! - Page 3 | This thread | Refback | 06-15-2007 11:59 AM |
| Digg / Programming / Upcoming | This thread | Refback | 06-15-2007 08:13 AM |
| Digg - MySQL Injection Attack Prevention in PHP Tutorial | This thread | Refback | 06-15-2007 05:18 AM |
| Digg / Programming / Upcoming | This thread | Refback | 06-14-2007 10:35 PM |
| Digg / Programming / Upcoming | This thread | Refback | 06-14-2007 08:42 PM |
| Digg / Technology / Upcoming | This thread | Refback | 06-14-2007 05:43 PM |