php - htmlentities to allow <a> links - How? -
in order make inputs safe, i'm using htmlentities in php:
$input = $_post['field']; $result = htmlspecialchars($input);
this works, realized in inputs, need allow basic markup <b>
, <i>
, copyright logos , basic stuff user. started doing this:
$result = $_post['ftext']; $presanitize = htmlspecialchars($result); $newftext = str_replace(array("<i>", "<b>", "</i>", "</b>", "©", """, "<a>", "</a>"), array("<i>", "<b>", "</i>", "</b>", "©", '"', "<a>", "</a>"), $presanitize);
now come main problem: how allow things <a>
, <img>
don't have tag , don't know comes inside of it?
i can replace , because it's , if replace , wont work i'll have lots of stuff (<a href="http://link.com">text</a>
) inside of it. should do? in advance.
the simple answer is: don't. that's part of reason why many popular forum systems use kind of markup that's not plain html. otherwise people can , nasty stuff way or another.
<img src="http://example.com/random-pic.jpg" onload="location.href='http://some.nasty.page/exploit';"/>
but can remove event tags? of course, keep date browsers support , quirks? can outsmart everyone?
if still want it, documented, tested, , used library or script provides functionality. php has built in, it's barebone. keywords "php html sanitizer" or similar.
personally i'd recommend support markdown or bbcode syntax (again: there many ready use snippets , libraries available). don't reinvent wheel unless have to.
Comments
Post a Comment