Answer the question
In order to leave comments, you need to log in
What is the best way for a self-written CMS to filter comment data before displaying it?
There is a site, "cms" - self-written. I plan to add the ability to add comments. Data from the form (text, no photos or files there) will be entered into the mysql database using the phpfaq.ru/safemysql class in a raw, unformatted form. The question is how to display this data (text of comments) in the safest way, so as not to get all sorts of xss and other things.
Previously, I did something like this:
$input_text = trim($input_text);
$input_text = strip_tags($input_text);
$input_text = htmlspecialchars($input_text);
$input_text = mysql_escape_string($input_text);
public function getOne()
{
$query = $this->prepareQuery(func_get_args());
if ($res = $this->rawQuery($query))
{
$row = $this->fetch($res);
if (is_array($row)) {
return reset($row);
}
$this->free($res);
}
return FALSE;
}
...
switch ($part)
{
case '?n':
$part = $this->escapeIdent($value);
break;
case '?s':
$part = $this->escapeString($value);
break;
...
}
...
private function rawQuery($query)
{
...
$res = mysqli_query($this->conn, $query);
...
}
Answer the question
In order to leave comments, you need to log in
1) Working with the database at a low level should be done through PDO. I like Doctrine DBAL for such purposes.
2) Escaping the output: htmlspecialchars($text, ENT_QUOTES, 'utf-8');
// EDIT
This is the approach used in Laravel.htmlentities($value, ENT_QUOTES, 'UTF-8', false);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question