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
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 12, 2014 at 15:43
\$\endgroup\$
6
  • 1
    \$\begingroup\$ mysql is deprecated. Instead use PDO or mysqli: php.net/manual/en/intro.mysql.php \$\endgroup\$ 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\$ 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\$ Commented May 12, 2014 at 15:54
  • \$\begingroup\$ i have add the code of the two functions \$\endgroup\$ Commented May 12, 2014 at 15:58

1 Answer 1

2
\$\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

answered May 12, 2014 at 15:48
\$\endgroup\$
3
  • \$\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\$ Both are easy. I would prefer PDO. \$\endgroup\$ Commented May 19, 2014 at 13:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.