Jump to content

htmlspecialchars


UnknownPlayer

Recommended Posts

Don't use htmlentities() to insert.  Use it when you want to display data but don't want HTML.  If you want to store legitimate HTML then store it as is.  There is no concern with storing HTML, the concern is when you display it in a browser.  If you don't want HTML then either reject it and don't insert it or striptags().

Link to comment
Share on other sites

Don't use htmlentities() to insert.  Use it when you want to display data but don't want HTML.  If you want to store legitimate HTML then store it as is.  There is no concern with storing HTML, the concern is when you display it in a browser.  If you don't want HTML then either reject it and don't insert it or striptags().

sorry, my answer was unclear, escape data to be inserted, use htmlentities to output.. however it would still be acceptable to use htmlentities before database insertion.. just wouldn't be as clean/organized.

Link to comment
Share on other sites

htmlspecialchars() is similar to htmlentities() except that it only translates 5 characters.  You don't need it if you use htmlentities().  And as I said before, you shouldn't use it on data going to the database.  Use it when displaying if you don't want the data rendered as HTML.  If the data should not contain HTML then use striptags() before insert.

Link to comment
Share on other sites

HTML entities has an optional charset argument (the 3rd argument) which you can use to specify a charset for the function to use in its conversion. It seems to me you have a charset problem, so check out the manual entry on htmlentities: http://php.net/manual/en/function.htmlentities.php

 

and look at the examples which specify a charset, and look at the charsets that PHP supports. I am not very knowledgeable on what characters are in what charsets, so this is just a guess, but ISO-8859-15 may be what you need

Link to comment
Share on other sites

Did you not read his post? You need to specify the charset when you call htmlentities

 

Better yet - don't use htmlentities(). Characters like 'žšđčć' don't need to be encoded assuming you've declared the page is UTF-8 in the META and your database uses UTF-8. Instead, use htmlspecialchars. It's safe by default to use with most common encodings.

 

Calling htmlspecialchars() on data you grab from the database ensures that someone hasn't put rouge data into your database, and avoided any checks you put in while inserting.

Link to comment
Share on other sites

I would not suggest using strip_tags, as you are leaving yourself open to any future vulnerabilities found in that function.

 

If you must have formatting, use BBCode. There's tons of support for it.

 

I suggest using XSS-prevention methods when you echo your data. If you do it on insertion, and someone finds a way to inject data that bypasses that insertion call, you will be open to XSS. Instead, perform the htmlspecialchars() or strip_tags() call when you want to output the data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.