Re: Extrange behavior with Header('Location')
| From: | Chris Shiflett | Date: | Tue, 21 Sep 2004 03:23:53 +0000 |
| Subject: | Re: Extrange behavior with Header('Location') | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-197381@lists.php.net to get a copy of this message | ||
--- Jordi Canals <jcanals@gmail.com> wrote:
> I have seen an strange thing with the funcion
> Header("Location: $url") and will tell in short words.
>
> I have a method in a class wich composes an URL from the
> database, this method sets some extra params in the url. In
> this case, the function returns:
>
> /myaccount/?opt=sys&id=3
>
> Note the & in the URL
>
> Well, with this code, the url works perfect and the & is
> going as expected redirecting to /myaccount/?opt=sys&id=3
>
> echo '<a href="'. $url .'">Test URL</a>';
There is no redirection taking place here. HTML entities are interpreted
by the browser. In fact, this is their whole purpose - for representing
characters properly in HTML.
> But when triying
>
> header("Location: $url");
>
> What I get is address is literally the & string and not the
> & char.
Yes, because when you use header(), you're setting a raw HTTP header. This
is not to be confused with HTML, which is included in the content of an
HTTP response.
> header('Location: '. html_entity_decode($url));
>
> I would like to know if there is a better way to do it
This should work fine. However, I would prefer to keep the data in its
original format and only use htmlentities() when I plan to display it
within HTML, leaving it unaltered otherwise.
Hope that helps.
Chris
=====
Chris Shiflett - http://shiflett.org/
PHP Security - O'Reilly
Coming December 2004
HTTP Developer's Handbook - Sams
http://httphandbook.org/
PHP Community Site
http://phpcommunity.org/