0
\$\begingroup\$

On my application I use this method to store and to output the data. I would like to know if it is safely and correct.

##store the data###
//sanitize
function clean($testo)
{
    $config = HTMLPurifier_Config::createDefault();
    $purifier = new HTMLPurifier($config);
    $testo = $purifier->purify($testo);

    $testo = mysql_real_escape_string($testo);
    return $testo;
}
$value = clean($_POST[value]);
//the clean function contain mysql_real_escape_string and htmlpurifier class
$sql = mysql_query("insert into table values(null,$value);");

##output the data####
$sql = mysql_query("select * from table");
$val = mysql_fetch_array($sql);
function echoValue($valore){
    $valore = htmlspecialchars(strip_tags(stripslashes($valore)), ENT_QUOTES, "UTF-8");
    return $valore;

}
echo echoValue($val);
// the echoValue function contain strip_tags and htmlentities
\$\endgroup\$
6
  • 1
    \$\begingroup\$ mysql is deprecated. Instead use PDO or mysqli: php.net/manual/en/intro.mysql.php \$\endgroup\$
    – Daniel Lisik
    Commented May 12, 2014 at 15:44
  • 1
    \$\begingroup\$ Read up on the subject because it is not. \$\endgroup\$ Commented May 12, 2014 at 15:45
  • 1
    \$\begingroup\$ And it is impossible to answer without knowing what clean looks like \$\endgroup\$
    – WizKid
    Commented May 12, 2014 at 15:46
  • \$\begingroup\$ Both functions seems to falsify the content. I don't consider this as a "correct" behaviour. No need to add something about using mysql_* functions in new code. \$\endgroup\$
    – VMai
    Commented May 12, 2014 at 15:54
  • \$\begingroup\$ i have add the code of the two functions \$\endgroup\$
    – user3348353
    Commented May 12, 2014 at 15:58

1 Answer 1

1
\$\begingroup\$

To "Safely" store data, please don't use mysql_* functions. Use PDO or mysqli.

Also, use PreparedStatements wherever you use "where" clause in queries. That will protect you against injection.

https://www.php.net/pdo.prepared-statements

\$\endgroup\$
4
  • \$\begingroup\$ it is a huge application, to convert everything into PDO will take long time, there is not other ways? to fix this code? \$\endgroup\$ Commented May 19, 2014 at 12:55
  • \$\begingroup\$ There's no other way. I personally did many such things to achieve security and clean code. Better be safe than sorry. \$\endgroup\$ Commented May 19, 2014 at 12:57
  • \$\begingroup\$ wich is more easy to convert mysqli o pdo? thanks \$\endgroup\$ Commented May 19, 2014 at 13:16
  • \$\begingroup\$ Both are easy. I would prefer PDO. \$\endgroup\$ Commented May 19, 2014 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.