Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify a php variable if there is a javascript code

Tags:

javascript

php

I have some variables in PHP (strings) and I vould like to check if inside of those strings I have some javascript code. If so, I would like to make this code inactive and be displayed as string on the website, not to be executed as javascript code.

This will be a kind of security method.

Is there a method to do it in php? If you could give me an example, it's even better.

Thank you very much for your help.

like image 639
Milos Cuculovic Avatar asked Jul 13 '26 14:07

Milos Cuculovic


2 Answers

You should be using htmlspecialchars() for any content you output into HTML. It escapes any HTML entities so that they are not taken literally. For example, < becomes &lt;. This also solves your problem.

http://php.net/manual/en/function.htmlspecialchars.php

like image 191
Brad Avatar answered Jul 15 '26 04:07

Brad


In addition to Brad's answer, consider reading the first answer here for a short summary and then the therein mentioned article afterwards.

This can help you to easily avoid outputting unsafe strings by accident.

like image 31
phant0m Avatar answered Jul 15 '26 04:07

phant0m