This is an example of a line that I should output in javascript alert ();
string with "double quote" in it
Since this line can be edited through PHP by my users, it is better to prevent XSS attacks. To do this in the HTML document of my document, I usually do:
<?php echo( htmlspecialchars( $MY_STRING, ENT_QUOTES, 'UTF-8' ) ); ?>
This works great.
But now I just noticed that if I output the same line in a javascript warning:
<script>
alert( "<?php echo( htmlspecialchars( $MY_STRING, ENT_QUOTES, 'UTF-8' ) ); ?>" );
</script>
Alert output in this case:
string with "double quote" in it
What is the best way to output double quotes as a warning, but also an XSS injection prefix?
source
share