I want to store all the data in UTF-8 so before storing
the input variables are converted to UTF-8 using
htmlspecialchars($var, ENT_QUOTES, 'UTF-8')
And then the database charset is set to UTF-8 and each table has the collation utf8_general_ci
However, When I store them, they all get saved as
Testing<ASC'a'sc'/A>
And are displayed as such.
I removed htmlspecialchars
function when I fetched the results.
But I still don't understand why it gets saved like that, I tried removing htmlspecialchars
in the $_POST
variables and then it saves it okay but then it comes out as HTML. For example if you fetched
<script>alert('lol')</script>
Which is not dangerous.
public function __construct() {
$protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV", "GLOBALS");
foreach($protected as $var)
{
if(isset($_POST[$var]) || isset($_GET[$var]) || isset($_COOKIE[$var]) || isset($_FILES[$var]))
{
die("Hacking attempt");
}
}
$this->strip_slashes_array($_REQUEST);
$this->parse_incoming($_REQUEST);
}
public function parse_incoming($array) {
foreach($array as $key => $value) {
if(!is_array($value)) {
$this->input[$key] = $value;
} else {
return;
}
}
}
function strip_slashes_array(&$array)
{
foreach($array as $key => $val)
{
if(is_array($array[$key]))
{
$this->strip_slashes_array($array[$key]);
}
else
{
$array[$key] = htmlspecialchars($array[$key], ENT_QUOTES, 'UTF-8');
}
}
}
and when I insert them I use
$class->input['key']
instead of $_POST['key']