PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Become a PHP Expert!

Join 308,847 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 3,120 people online right now. Registration is fast and FREE... Join Now!





SQL Sanitize

"Sanitize" a string of SQL code to prevent SQL injection.

Submitted By: sandman85048
Actions:
Rating:
Views: 8,835

Language: PHP

Last Modified: December 31, 1969

Snippet


  1. /*
  2. Function: sql_sanitize( $sCode )
  3. Description: "Sanitize" a string of SQL code to prevent SQL injection.
  4. Parameters: $sCode: The SQL code which you wish to sanitize.
  5. Example: mysql_query('UPDATE table SET value="' . sql_sanitize("' SET id='4'") . '" WHERE id="1"');
  6. Requirements: PHP version 4 or greater
  7. */
  8. function sql_sanitize( $sCode ) {
  9.         if ( function_exists( "mysql_real_escape_string" ) ) { // If PHP version > 4.3.0
  10.                 $sCode = mysql_real_escape_string( $sCode ); // Escape the MySQL string.
  11.         } else { // If PHP version < 4.3.0
  12.                 $sCode = addslashes( $sCode ); // Precede sensitive characters with a slash \
  13.         }
  14.         return $sCode; // Return the sanitized code
  15. }

Copy & Paste


Comments


capoenkz 2009-01-30 08:26:52

how to use it??? i'm new here.... thx

huzi8t9 2009-04-07 20:30:23

CODE
$code = '<script language="javascript"> body.onload = top.location = "http://www.google.co.uk" </script>'; //^^Code submitted by a bad user $new_code = sql_sanitize($code); //insert your code to the database. mysql_query("INSERT INTO table (comment) VALUES('$new_code');");
Hope this helps. Also, I hope it's right :)


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month