I have a javascript-based rich text editor.
What is the safest way to save the tags it generates?
I'm using MySQL as my database.
I'm not sure if using mysql_real_escape_string($text);
is safe.
I have a javascript-based rich text editor.
What is the safest way to save the tags it generates?
I'm using MySQL as my database.
I'm not sure if using mysql_real_escape_string($text);
is safe.
I'd recommend that you use mysqli
to interface with the database, that way you don't need to escape data and get a complete protection against SQL injections. You may of course still need to protect against HTML injections.
You do not need to write the string of text to a JavaScript file. If you need it to be handled by JavaScript I suggest that you fetch the data using an XMLHttpRequest (which contrary to what the name suggests does not require your data to be in XML form).
I can't think of a reason to use htmlentities
here. mysql_real_escape_string
is vital here because it prevents people from injecting malicious SQL code like ';DROP * FROM table foo;--
into you database. I'd try it without htmlentities
, if you find that you need to convert to entities then you could try htmlspecialchars
instead which only converts special characters.
If you want to limit the allowed HTML in your form you might also want to look into the strip_tags
function.
Relevant documentation:
http://us2.php.net/manual/en/function.htmlentities.php
http://us2.php.net/manual/en/function.htmlspecialchars.php
Good luck