PHP Protection Database Specific code example _php tutorial

Source: Internet
Author: User
Tags sql injection example
As the database mismanagement leads to data loss, there are no fewer examples of losses for yourself. We're going to talk this timeThe following code shows a sample script that runs the SQL statement. In this case, the SQL statement is a dynamic statement that allows the same attack. The owner of this form may consider the form to be safe because they have qualified the column name as the selection list. However, the code neglects the last habit of form spoofing-the code restricting the option to a drop-down box does not mean that others will not be able to publish a form that contains the required content (including asterisks [*]).

 
 
  1. < HTML >
  2. < Head >
  3. < title > SQL Injection Example title>
  4. Head >
  5. < Body >
  6. < form id="Myfrom" action=" "
  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. $ Col = $_post[' col '];
  25. $ Select = "SELECT" . $col. "From Account_data WHERE account_number ="
  26. . $_post[' Account_number '. ";" ;
  27. Echo ' <p> '. $select. ' p> ';
  28. $ result = mysql_query ($select) or Die ('<p>'. Mysql_error (). ' p> ');
  29. Echo ' <table> ';
  30. While ($row = mysql_fetch_assoc($result)) {
  31. Echo ' <tr> ';
  32. Echo ' <td> '. $row [$col]. ' td> ';
  33. Echo ' tr> ';
  34. }
  35. Echo ' table> ';
  36. Mysql_close ($link);
  37. }
  38. ?>
  39. Body >
  40. HTML >

Therefore, in order to form the PHP protection database habit, avoid using dynamic SQL code whenever possible. If you cannot avoid dynamic SQL code, do not use input directly on the column. The following shows that in addition to using static columns, you can also add a simple validation routine to the account number field to ensure that the input value is not a non-numeric value.

 
 
  1. < HTML >
  2. < Head >
  3. < title > SQL Injection Example title>
  4. Head >
  5. < Body >
  6. < form id="Myfrom" action=" "
  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. /* Remember habit #1--validate your data! */
  20. if (Isset ($_post[' Account_number ')) &
  21. Isvalidaccountnumber ($_post[' Account_number ')) {
  22. /* Do the form processing */
  23. $ Link = mysql_connect (' hostname ', ' user ', ' password ') or
  24. Die (' Could not Connect '. Mysql_error ());
  25. mysql_select_db (' Test ', $link);
  26. $ Select = sprintf ("Select Account_number, Name, Address".)
  27. "from Account_data WHERE Account_number =%s; ",
  28. Mysql_real_escape_string ($_post[' account_number '));
  29. Echo ' <p> '. $select. ' p> ';
  30. $ result = mysql_query ($select) or Die ('<p>'. Mysql_error (). ' p> ');
  31. Echo ' <table> ';
  32. While ($row = mysql_fetch_assoc($result)) {
  33. Echo ' <tr> ';
  34. Echo ' <td> '. $row [' Account_number ']. ' td> ';
  35. Echo ' <td> '. $row [' name ']. ' td> ';
  36. Echo ' <td> '. $row [' address ']. ' td> ';
  37. Echo ' tr> ';
  38. }
  39. Echo ' table> ';
  40. Mysql_close ($link);
  41. } else {
  42. echo " <span style= "font-color:red">".
  43. "supply a valid account number! span> ";
  44. }
  45. }
  46. ?>
  47. Body >
  48. HTML >

The use of the mysql_real_escape_string () function is also shown in this example of the PHP protection database. This function will correctly filter your input, so it does not include invalid characters. If you have been relying on MAGIC_QUOTES_GPC, be aware that it has been deprecated and will be removed in PHP V6. You should avoid using it from now on and write a secure PHP application in this case. In addition, if you are using an ISP, it is possible that your ISP does not have MAGIC_QUOTES_GPC enabled.

Finally, in the improved PHP protection database example, you can see that the SQL statement and output do not include dynamic column options. Using this method, you can output columns if you add them to a table that later contains different information. If you want to use a framework to work with a database, your framework might have performed SQL validation for you. Ensure that the document is consulted to ensure the security of the framework, and if you are still unsure, verify to ensure that it is secure. Even if you use the framework for database interaction, you still need to perform additional validation.


http://www.bkjia.com/PHPjc/446396.html www.bkjia.com true http://www.bkjia.com/PHPjc/446396.html techarticle As the database mismanagement leads to data loss, there are no fewer examples of losses for yourself. We're going to talk about this. The following code shows a sample script that runs an SQL statement. In this case ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.