I am using the htmlspecialchars() function to prevent XSS attacks. I doubt that the best way to store data in a database is from the following.
Method 1: Save user-entered values ββafter applying the htmlspecialchars() function. Using this, user input "<script>" will become "& lt; script & gt;".
Method 2: Keep user input as is and use the htmlspecialchars() method when retrieving data and displaying it on the page.
The reason for my doubt is that I believe that using method 1 there will be overhead in the database, while using method 2 data needs to be converted again and again on demand via php. So I'm not sure which one is better.
For more information, I use htmlspecialchars($val, ENT_QUOTES, "UTF-8") to convert 'and' as well.
Please help me clear my doubts. Also explain if possible.
Thanks.
source share