Advertisement
reenadak

sanitize input

Feb 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1.     /*
  2.     - Sanitize Input
  3.     - Helps reduce security risks and minimalises the possibility of SQL injection.
  4.    
  5.     $string     - The string you wish to sanitize before entering into the database.
  6.     $level      - The level of security:
  7.                    > 1 = adds slashes to prevent SQL injection
  8.                    > 2 = adds slashes + htmlspecialchars to turn HTML tags into
  9.                          HTML entities (eg. <html> becomes &lt;html&gt;)
  10.                    > 3 = add slashes + strip_tags to remove all HTML tags.
  11.     */
  12.    
  13.     public function sanitizeInput($string, $level = 1) {
  14.        
  15.         switch ($level) {
  16.         case 1:
  17.             $string = addslashes($string);
  18.             break;
  19.         case 2:
  20.             $string = htmlspecialchars(addslashes($string));
  21.             break;
  22.         case 3:
  23.             $string = strip_tags(addslashes($string));
  24.             break;
  25.         }
  26.        
  27.         return $string;
  28.        
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement