Input
suggest change- Check that a received string is valid UTF-8 before storing or using it.
mb_check_encoding()does that. A client can send bytes in any encoding.
$string = $_REQUEST['user_comment'];
if (!mb_check_encoding($string, 'UTF-8')) {
// the string is not UTF-8, so re-encode it.
$actualEncoding = mb_detect_encoding($string);
$string = mb_convert_encoding($string, 'UTF-8', $actualEncoding);
}
- An HTML5 form is submitted in the page's character encoding.
accept-charset="UTF-8"asks for UTF-8 even when the page uses another encoding. Browsers may ignore that attribute, so still check the bytes:
<form action="somepage.php" accept-charset="UTF-8">
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents