Jump to content

Encoding & Decoding HTML when working with MySQL


NEONecd999

Recommended Posts

I have a form with a text area where users can input an HTML code. That is then stored in a field of a mysql table.

 

On another page, this HTML code must be selected from the table and echoed, so that the processed HTML code is what will appear in the browser.

 

Problem is, there are symbols in the HTML code, such as single quotation marks, that will kill the syntax of the mysql UPDATE statement and allow for mysql injection attacks. To avoid this, i usually put "htmlentities($content, ENT_QUOTES)" instead of simply "$content" into the MySQL statement. However, if I do this, when I SELECT the info to output in another HTML page, it is not processed by the HTML, as all HTML symbols have been translated into their character codes...

 

So which PHP functions should I use to encode and decode my content (in this case HTML code) to suit this scenario?

 

Thanks.

Link to comment
Share on other sites

So which PHP functions should I use to encode and decode my content (in this case HTML code) to suit this scenario?

 

Dont. Use mysql_real_escape_string

 

However, you might want to consider what you're allowing through - unless this is something hidden away in an admin area, someone could use things like script tags and you'll end up in a whole heap of trouble.

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.