\$\begingroup\$
\$\endgroup\$
6
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
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
3
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.
answered May 12, 2014 at 15:48
-
\$\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\$user3348353– user33483532014年05月19日 12:55:25 +00:00Commented 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\$Sasanka Panguluri– Sasanka Panguluri2014年05月19日 12:57:06 +00:00Commented May 19, 2014 at 12:57
-
\$\begingroup\$ Both are easy. I would prefer PDO. \$\endgroup\$Sasanka Panguluri– Sasanka Panguluri2014年05月19日 13:29:36 +00:00Commented May 19, 2014 at 13:29
default
mysql
is deprecated. Instead usePDO
ormysqli
: php.net/manual/en/intro.mysql.php \$\endgroup\$