cberube09 Posted March 30, 2009 Share Posted March 30, 2009 I am trying to encode an ampersand as & within the php header("Location: "); . This method works fine in normal links in php but in the header it passes to the browser as the encoded form & in the url. How do I encode an ampersand in the url of a header redirect? Quote Link to comment Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 What do you mean? Like header("Location: http://google.com/?blah=bleh")? Or like header("Location: http://google.com?blah=bleh%26bleck"); %26 is & escaped. Quote Link to comment Share on other sites More sharing options...
cberube09 Posted March 30, 2009 Author Share Posted March 30, 2009 The only problem is that the browser url shows the &26 and not the & so the page is not found. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 What URL are you trying to do? (Leave off the domain if you want.) Also, it's %26, not &26. Quote Link to comment Share on other sites More sharing options...
cberube09 Posted March 30, 2009 Author Share Posted March 30, 2009 Right, sorry, that what I meant, I just wrote the wrong thing. This is the code with the url unencoded header("Location:index.php?content=main&id=$categoryout&title=$titleout"); When I use anything like & or %26, that is what shows up in the URL when it redirects, not the regular &, so in turn, the page is not found. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 Try: header("Location: index.php?content=main&id=" . urlencode($categoryout) . "&title=" . urlencode($titleout)); And technically it should be something like: header("Location: http://{$_SERVER['HTTP_HOST']}/index.php?content=main&id=" . urlencode($categoryout) . "&title=" . urlencode($titleout)); Since the HTTP specification says that Location is to specify a full URL, domain included. Quote Link to comment Share on other sites More sharing options...
cberube09 Posted March 31, 2009 Author Share Posted March 31, 2009 Didn't know about the full address, but I'm trying to encode the &s between main and id and between categoryout and title, not the variables i am including Quote Link to comment Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 Errrr.... Why are you trying to encode those? What is the URL that you want to appear in the address bar, without the domain? Quote Link to comment Share on other sites More sharing options...
cberube09 Posted April 2, 2009 Author Share Posted April 2, 2009 Something like this... index.php?content=main&id=Government_Policy&title=Hypocritical_Politicians I want to encode the ampersands because that this is required by W3 specification. Since the ampersand does not mean AND but is an operator in the link, it must be encoded. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted April 2, 2009 Share Posted April 2, 2009 I'm not sure if thats neccessary for headers, I think it still works ok un-encoded. I think its just the anchor href that needs to be url-encoded. Quote Link to comment Share on other sites More sharing options...
cberube09 Posted April 2, 2009 Author Share Posted April 2, 2009 Thanks, that's good to know, because I could not get it to work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.