PHP database protection code example _ PHP Tutorial

Source: Internet
Author: User
Tags sql injection example
Sample code for PHP database protection. Data loss is caused by poor database management, and there are no fewer examples of loss for you. The following code shows the sample script for running SQL statements. In this example, data loss is caused by poor database management, and there are no fewer examples of loss for you. We will talk about it this time.The following code shows the sample script for running SQL statements. In this example, SQL statements are dynamic statements that allow the same attacks. The owner of this form may think that the form is safe because they have defined the column name as a selection list. However, the code ignores the last habit of form spoofing-limiting the option to a drop-down box does not mean that others cannot publish a form containing the required content (including the asterisk [*]).

 
 
  1. <html>
  2. <head>
  3. <title>SQL Injection Example title>
  4. head>
  5. <body>
  6. <form id="myFrom" action=" "
  7. method="post">
  8. <p><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" /> p>
  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, to form the habit of using PHP to protect the database, avoid using dynamic SQL code as much as possible. If dynamic SQL code cannot be avoided, do not directly use the input for the column. The following shows that in addition to using static columns, you can also add a simple verification 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. <p><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" /> p>
  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. "Please supply a valid account number! span>";
  44. }
  45. }
  46. ?>
  47. body>
  48. html>

This PHP database protection example also shows the usage of the mysql_real_escape_string () function. This function filters your input correctly, so it does not include invalid characters. If you have been dependent on magic_quotes_gpc, you must note that it has been discarded and will be deleted in PHP V6. Avoid using it from now on and write secure PHP applications in this case. In addition, if ISP is used, it is possible that magic_quotes_gpc is not enabled for your ISP.

Finally, in the improved PHP database protection example, you can see that the SQL statement and output do not include the dynamic column option. If you add columns to a table that later contains different information, you can output these columns. If you want to use the framework in combination with the database, your framework may have performed SQL verification for you. Ensure that documents are reviewed to ensure the security of the framework. if you are still unsure, verify the documents to ensure security. Other verification is required even if the framework is used for database interaction.


Bytes. The following code shows the sample script for running SQL statements. In this example...

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.