Link to home
Start Free TrialLog in
Avatar of Kinderly Wade
Kinderly WadeFlag for United States of America

asked on

Why and when to use htmlentities and htmlspecialchars

Dear experts,

I know what htmlentites and htmlspecialchars do as in converting <, >, &, etc... into &lt, &gt etc..

I wish to know when to use these functions and shall I store conveted values such as &lt, &gt etc. into database?

Thanks
SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do not store the converted values in the database.  Store the originals.

Use these functions whenever your script creates output from user input.  Typically this would be in the View component of the MVC design pattern.  The idea is to make stray and unwanted HTML and JavaScript into something that is safe for the client browsers.  If you do not do this, the client browser will run JavaScript when you send the JavaScript, and that may include doing some rather nasty things to the clients.
Avatar of Kinderly Wade

ASKER

Hi experts,

If I don't store the converted value into database, what can I do to prevent a sql injection? I am trying to convert characters in away that can be safely stored into database with sql injection prevention. Thanks.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial