首页 > php框架 > PHP保护数据库的具体代码示例

PHP保护数据库的具体代码示例

因为数据库管理不善导致数据丢失,为自己带来损失的例子不再少数。我们这次就要讲到
下面代码显示了运行 SQL 语句的示例脚本。在本例中,SQL 语句是允许相同攻击的动态语句。此表单的所有者可能认为表单是安全的,因为他们已经把列名限定为选择列表。但是,代码疏忽了关于表单欺骗的最后一个习惯 — 代码将选项限定为下拉框并不意味着其他人不能够发布含有所需内容的表单(包括星号 [*])。

  1. <html>   
  2. <head>   
  3. <title>SQL Injection Example</title>   
  4. </head>   
  5. <body>   
  6. <form id=myFrom action=<?php echo $_SERVER[PHP_SELF]; ?>   
  7.     method=post>   
  8. <div><input type=text name=account_number   
  9.     value=<?php echo(isset($_POST[account_number]) ?    
  10.         $_POST[account_number] : ); ?> />   
  11. <select name=col>   
  12. <option value=account_number>Account Number</option>   
  13. <option value=name>Name</option>   
  14. <option value=address>Address</option>   
  15. </select>   
  16. <input type=submit value=Save name=submit /></div>   
  17. </form>   
  18. <?php   
  19. if ($_POST[submit] == Save) {   
  20.     /* do the form processing */   
  21.     $link = mysql_connect(hostname, user, password) or    
  22.         die (Could not connect . mysql_error());   
  23.     mysql_select_db(test, $link);   
  24.            
  25.         $col = $_POST[col];   
  26.  
  27.     $select = SELECT  . $col .  FROM account_data WHERE account_number =     
  28.         . $_POST[account_number] . ; ;   
  29.     echo <p> . $select . </p>;   
  30.  
  31.     $result = mysql_query($select) or die(<p> . mysql_error() . </p>);   
  32.  
  33.     echo <table>;   
  34.     while ($row = mysql_fetch_assoc($result)) {   
  35.         echo <tr>;   
  36.         echo <td> . $row[$col] . </td>;   
  37.         echo </tr>;   
  38.     }   
  39.     echo </table>;   
  40.  
  41.     mysql_close($link);   
  42. }   
  43. ?>   
  44. </body>   
  45. </html>  

因此,要形成PHP保护数据库的习惯,请尽可能避免使用动态 SQL 代码。如果无法避免动态 SQL 代码,请不要对列直接使用输入。下面则显示了除使用静态列外,还可以向帐户编号字段添加简单验证例程以确保输入值不是非数字值。

  1. <html>   
  2. <head>   
  3. <title>SQL Injection Example</title>   
  4. </head>   
  5. <body>   
  6. <form id=myFrom action=<?php echo $_SERVER[PHP_SELF]; ?>   
  7.     method=post>   
  8. <div><input type=text name=account_number   
  9.     value=<?php echo(isset($_POST[account_number]) ?    
  10.         $_POST[account_number] : ); ?> /> <input type=submit   
  11.     value=Save name=submit /></div>   
  12. </form>   
  13. <?php   
  14. function isValidAccountNumber($number)    
  15. {   
  16.     return is_numeric($number);   
  17. }   
  18. if ($_POST[submit] == Save) {   
  19.  
  20.     /* Remember habit #1--validate your data! */   
  21.     if (isset($_POST[account_number]) &   
  22.     isValidAccountNumber($_POST[account_number])) {   
  23.  
  24.         /* do the form processing */   
  25.         $link = mysql_connect(hostname, user, password) or   
  26.         die (Could not connect . mysql_error());   
  27.         mysql_select_db(test, $link);   
  28.  
  29.         $select = sprintf(SELECT account_number, name, address  .   
  30.          FROM account_data WHERE account_number = %s;,   
  31.         mysql_real_escape_string($_POST[account_number]));   
  32.         echo <p> . $select . </p>;   
  33.         $result = mysql_query($select) or die(<p> . mysql_error() . </p>);   
  34.  
  35.         echo <table>;   
  36.         while ($row = mysql_fetch_assoc($result)) {   
  37.             echo <tr>;   
  38.             echo <td> . $row[account_number] . </td>;   
  39.             echo <td> . $row[name] . </td>;   
  40.             echo <td> . $row[address] . </td>;   
  41.             echo </tr>;   
  42.         }   
  43.         echo </table>;   
  44.  
  45.         mysql_close($link);   
  46.     } else {   
  47.         echo <span style=font-color:red> .   
  48.     Please supply a valid account number!</span>;   
  49.  
  50.     }   
  51. }   
  52. ?>   
  53. </body>   
  54. </html>   

在这次PHP保护数据库的例子中还展示了 mysql_real_escape_string() 函数的用法。此函数将正确地过滤您的输入,因此它不包括无效字符。如果您一直依赖于 magic_quotes_gpc,那么需要注意它已被弃用并且将在 PHP V6 中删除。从现在开始应避免使用它并在此情况下编写安全的 PHP 应用程序。此外,如果使用的是 ISP,则有可能您的 ISP 没有启用 magic_quotes_gpc。

最后,在改进的PHP保护数据库示例中,您可以看到该 SQL 语句和输出没有包括动态列选项。使用这种方法,如果把列添加到稍后含有不同信息的表中,则可以输出这些列。如果要使用框架以与数据库结合使用,则您的框架可能已经为您执行了 SQL 验证。确保查阅文档以保证框架的安全性;如果仍然不确定,请进行验证以确保稳妥。即使使用框架进行数据库交互,仍然需要执行其他验证。


本文地址:http://www.phprm.com/frame/php1003573.html

转载随意,但请附上文章地址:-)

标签:none

发表留言