Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. <?php
  2. // These variables define the connection information for your MySQL database
  3. $username = "root";
  4. $password = "admin";
  5. $host = "localhost";
  6. $dbname = "restaurant";
  7.  
  8. // UTF-8 is a character encoding scheme that allows you to conveniently store
  9. // a wide varienty of special characters, like ¢ or €, in your database.
  10. // By passing the following $options array to the database connection code we
  11. // are telling the MySQL server that we want to communicate with it using UTF-8
  12. // See Wikipedia for more information on UTF-8:
  13. // http://en.wikipedia.org/wiki/UTF-8
  14. $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
  15.  
  16. // A try/catch statement is a common method of error handling in object oriented code.
  17. // First, PHP executes the code within the try block. If at any time it encounters an
  18. // error while executing that code, it stops immediately and jumps down to the
  19. // catch block. For more detailed information on exceptions and try/catch blocks:
  20. // http://us2.php.net/manual/en/language.exceptions.php
  21. try
  22. {
  23. // This statement opens a connection to your database using the PDO library
  24. // PDO is designed to provide a flexible interface between PHP and many
  25. // different types of database servers. For more information on PDO:
  26. // http://us2.php.net/manual/en/class.pdo.php
  27. $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
  28. }
  29. catch(PDOException $ex)
  30. {
  31. // If an error occurs while opening a connection to your database, it will
  32. // be trapped here. The script will output an error and stop executing.
  33. // Note: On a production website, you should not output $ex->getMessage().
  34. // It may provide an attacker with helpful information about your code
  35. // (like your database username and password).
  36. die("Failed to connect to the database: " . $ex->getMessage());
  37. }
  38.  
  39. // This statement configures PDO to throw an exception when it encounters
  40. // an error. This allows us to use try/catch blocks to trap database errors.
  41. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  42.  
  43. // This statement configures PDO to return database rows from your database using an associative
  44. // array. This means the array will have string indexes, where the string value
  45. // represents the name of the column in your database.
  46. $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  47.  
  48. // This block of code is used to undo magic quotes. Magic quotes are a terrible
  49. // feature that was removed from PHP as of PHP 5.4. However, older installations
  50. // of PHP may still have magic quotes enabled and this code is necessary to
  51. // prevent them from causing problems. For more information on magic quotes:
  52. // http://php.net/manual/en/security.magicquotes.php
  53. if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
  54. {
  55. function undo_magic_quotes_gpc(&$array)
  56. {
  57. foreach($array as &$value)
  58. {
  59. if(is_array($value))
  60. {
  61. undo_magic_quotes_gpc($value);
  62. }
  63. else
  64. {
  65. $value = stripslashes($value);
  66. }
  67. }
  68. }
  69.  
  70. undo_magic_quotes_gpc($_POST);
  71. undo_magic_quotes_gpc($_GET);
  72. undo_magic_quotes_gpc($_COOKIE);
  73. }
  74.  
  75. // This tells the web browser that your content is encoded using UTF-8
  76. // and that it should submit content back to you using UTF-8
  77. header('Content-Type: text/html; charset=utf-8');
  78.  
  79. // This initializes a session. Sessions are used to store information about
  80. // a visitor from one web page visit to the next. Unlike a cookie, the information is
  81. // stored on the server-side and cannot be modified by the visitor. However,
  82. // note that in most cases sessions do still use cookies and require the visitor
  83. // to have cookies enabled. For more information about sessions:
  84. // http://us.php.net/manual/en/book.session.php
  85. session_start();
  86.  
  87. // Note that it is a good practice to NOT end your PHP files with a closing PHP tag.
  88. // This prevents trailing newlines on the file from being included in your output,
  89. // which can cause problems with redirecting users.
  90.  
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement