I've working on some security stuff, and php is giving me some odd behaviour. Could you help me figure out what's going on? :)
So, I have an array of inputs, like so
$first_name = "<script>alert();</script>";
$middle_name = 'Robert';
$last_name = 'Smith';
$username = 'testusername1';
$email = "testemail@mail.com";
$password = 'banana1';
and I am testing them for XSS, using htmlspecialchars, like this.
$first_name = htmlspecialchars($first_name, ENT_QUOTES, 'UTF-8');
Which works just fine to stop the script in the $first_name running.
However, paste this code into a foreach loop, and the javascript alert runs.
Here is my current foreach loop (not working properly)
$strings =
array($first_name,$middle_name,$last_name,$username,$email,$password);
foreach($strings as $string) {
$string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
I'm not sure what I'm missing here. I guess it's something to do with assigning the converted string back into the array? But that sounds so confusing it just doesn't feel like the right answer. Idk.
Thank you for your help.
Andrew