I have always been very confused with URL/HTML Escaping. More recently I looked deeper into it. Then looking at the PHP Docs for urlencode
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
I then realized that theres & in most query strings that seems like should be escaped. But it seems to work without escaping. I wonder why, and if its actually required.
Escaping
&into&is required in HTML, but it works in most browsers anyway. If it wouldn’t, 90% of the Internet would break. 🙂 It still is good style to escape ampersands, and it is required for the document to pass validation.See this W3C document for some good background why (the text focuses on a specific behaviour of PHP, but that doesn’t really matter): Ampersands, PHP Sessions and Valid HTML. Money quote (emphasis mine):