Objective response !
strip_tags() allows other types of insertion of XSS then use htmlspecialchars()
echo htmlspecialchars($Variavel, ENT_QUOTES, 'UTF-8');
Long answer.....
Let’s start by parts, what you want and protect yourself from the XSS, its data input is enabling the injection of commands or markup, we will analyze this well to be able to create a 100% functional solution, in security we must be calculating.
Analyzing the scenario where XSS can happen!
Let’s put two scenarios for this situation
<input type="text" value="$Suspeito">
and another common good is when we put the content in a div
<div class="container" id="ct">
<?php echo $Suspeito ?>
</div>
Now, let’s replace this variable with a classic xss
Doesn’t work !
<input type="text" value="<script> alert("Xss here");</script> ">
Works !
<div class="container" id="ct">
<script> alert("Xss here");</script>
</div>
Solution at first sight!
We can add extra protection using strip_tags() to escape tags !
Let’s see how it would look if our variable had a protection with strip_tags()
Doesn’t work !
<input type="text" value="alert("Xss here"); ">
Doesn’t work !
<div class="container" id="ct">
alert("Xss here");
</div>
Just like in Brazil, there is a way for everything and this solution is far from solving any problem, just use your imagination.
imagine this situation, apparently there are no problems right ?
Wrong, let’s force our imagination, I need to fuck with this code without using tags
<?php $Suspeito = '" onfocus=document.write("");" fecha="';
<input type="text" value="" onfocus=document.write("");" fecha="">
Oops, now it’s a problem Is it just that ?
';alert(String.fromCharCode(88,83,83))//';alert(String.fromCharCode(88,83,83))//";
alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//--
></SCRIPT>">'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>
'';!--"<XSS>=&{()}
<IMG SRC="javascript:alert('XSS');">
<IMG SRC=javascript:a&
#0000108ert('XSS')>
So then we need another solution that doesn’t fail that way, remembering that we have another problem, if you escape the tags, no one will be able to use... remember math, comparisons, or even code, you will simply lose data...
More flexible solution
By limiting encoding to UTF-8 and using htmlspecialchars, it becomes possible to use tags and scripts in html without having any effect on the page, the characters are disubstituted by <> entities will turn <> fully transparent to the user.
echo htmlspecialchars($Suspeito, ENT_QUOTES, 'UTF-8');
Look at this link showing a comparison between htmlspecialchars vs strip_tags Online script
This is maybe not a perfect solution, maybe there is a way to cheat, try to do it whenever you come across some security issue, ? "
But I am using and not returning anything <?php echo htmlspecialchars($exibe_tasks , ENT_QUOTES, 'UTF-8'); ?>
– Josimara
I updated the example using an array, see http://ideone.com/Er5UZZ
– Isvaldo Fernandes