I am using htmlentities($data, ENT_QUOTES)
on any data fetched from database before displaying it.
Is there a way I can set the flag ENT_QUOTES
by default for htmlentities()
function, so that even if I write htmlentities($data)
it should work as htmlentities($data, ENT_QUOTES)
.
As written in the documentation of php the default is ENT_COMPAT | ENT_HTML401
.
For your information I am using codeigniter framework, php5.
UPDATE 1: wrapping with a custom function as suggested by Michael could help but I have already used this everywhere in the website without ENT_QUOTES flag and was wondering if there is a way provided by php to change defaults for its functions.
UPDATE 2: I think html_escape() inbuilt function provided by codeigniter (suggested by Wesley) is the best for me so that i don't have to write my own wrapper function.
htmlentities() Function: The htmlentities() function is an inbuilt function in PHP that is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entities.
The only difference between htmlspecialchars() and htmlentities() function is that htmlspecialchars() function converts the special characters to HTML entities, whereas htmlentities() function converts all the applicable characters to html entities.
You use htmlspecialchars EVERY time you output content within HTML, so it is interpreted as content and not HTML. If you allow content to be treated as HTML, you have just opened the door to bugs at a minimum, and total XSS hacks at worst.
There's no way to change the default flags that I know of, but the advice given to you in the comments is absolutely the best way to approach this anyways: use a wrapper function.
Conveniently, Codeigniter has one built in already, appropriately named:
echo html_escape($string);
You can pass in arrays as well, here's what it does:
/**
* Returns HTML escaped variable
*
* @access public
* @param mixed
* @return mixed
*/
if ( ! function_exists('html_escape'))
{
function html_escape($var)
{
if (is_array($var))
{
return array_map('html_escape', $var);
}
else
{
return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
}
}
}
Just do a search for htmlentities
in your project and replace (carefully) with html_escape
. This will also provide the opportunity for you to easily make changes in the future because you can alter the function. It's a little bit of an initial time investment but well worth it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With