PHP MySQL Format Functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blyxx86
    Contributor
    • Nov 2006
    • 258

    PHP MySQL Format Functions

    I have these two functions that are used to sanitize data going into my database as well as format it for output.

    I have tested them with strings like "c:\r\new\" to verify that lines aren't messed up.

    These may be helpful to someone else, but please let me know if there are problems with them.

    Code:
     /**
     *
     * Sanitizes data that is passed to it for entry into mysql database.
     * Prevents sql injection, etc.
     *
     * @input = Data to be cleaned
     */
    function sanitize($input)
    {
    	if(is_array($input))
    	{
    		foreach($input as $key => $val)
    		{
    			$input[$key] = sanitize($val);
    		}
    	} else {	
    		$input = trim($input);
    		$input = htmlentities($input);
    		$input = str_replace("\r\n", "\n", $input);
    		$input = str_replace("\r", "\n", $input);
    		$input = str_replace("\n", "<newline>", $input);
    		$input = mysql_real_escape_string($input);
    	}
    	return $input;
    }
    
     /**
     *
     * Formats data that has been sanitized to display on the screen.
     *
     * @str (mixed) = Data to be formatted
     * @breaks (string) = What will replace standard newline characters (ie '&lt;br/>\n')
     */
    function fout($str, $breaks="<br />")
    {
    	if(is_array($str))
    	{
    		foreach($str as $key => $val)
    		{
    			$str[$key] = format_out($val, $breaks);
    		}
    	} else {
    		$str = str_replace("<newline>", $breaks."\n", $str);
    		$str = stripslashes($str);
    	}
    	return $str;
    }
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Hi, blyxx86.

    I've moved this thread into the editors corner, because it isn't really a question (which the answers forum is intended for).

    However, if you would like to document the post, add a bit of explanation to it, we could move it into the 'insights' area as an article.

    Looking over it, I don't see any problems with it.

    However, mysql_real_esca pe_string() and some other functions like it still allow for SQL injection - this was recently brought to my attention (see: http://en.wikipedia.org/wiki/SQL_inj...zed_Statements).

    You should try and use Parameterized Statements to completely wittle out any harmful attack on your DB.

    Edit: I know you use CI, so have a look at it's DB library, especially the active record pattern capabilities.



    Edit 2: http://codeigniter.com/user_guide/database/queries.html Go to the bottom for query binding.

    Comment

    • blyxx86
      Contributor
      • Nov 2006
      • 258

      #3
      It appears the mysql_real_esca pe_string bug has been fixed for anything newer than 5.0.21 according to their site.


      I do like parameterized statements though. I plan on implementing them in the model section of my application.

      Comment

      Working...