Forum Moderators: coopster
I got going and everything was working well till I found out that pages with a '&' in them were not redirecting properly.
I am a novice to programming and PHP so I need a bit og guidance. Here is the stripped down version of the script.
<?php
if($link == "")
die("Exasperated Hamster :: You must give me something to redirect");
header("Location: $link");
?>
So the script would work like this:
Link to scriptpage.php?link=http://www.somesite.com and it redirects.
Now if I call the script with a link like this: [somesite.com...]
it will only recirect the 'http://www.somesite.com/index.php?variable' and will leave off '&otherstuff'
Can anyone tell me what I can do to make all the subsequent variables and such in the links be ignored? It needs to be able to support all URL types on the web.
After reading further, it looks like you need to also use htmlentities() [php.net].
Example:
<?php
$link = htmlentities(url_encode("http://www.site.com?foo=bar&var=val"));
print <<<LINK
<a href="scriptpage.php?link=$link">Go</a>
LINK;
?>
I did notice, however, that your keys have no values. Are you sure your don't what something like:
[somesite.com...]
<It hasn't made a difference. If an & is found in the link it truncates from that point on.>
The problem is not with your redirect script, but instead with the URL that points to it. Your intention seems to be to pass a variable to a redirect script, and this variable is another URL which itself includes variables, right? If I got that part right, keep reading :-).
Let's say you want to redirect to:
[page.com...]
So if the link to your redirect script looked like this:
[main.com...]
This obviously will not work, because the browser does not get that the entire portion of the first question mark is supposed to be one big variable. Instead, it's broken up into smaller variabled with each & symbol, and perhaps the second question mark is not really liked much either.
So with the variable that points to the new page (and all of the it's variables), you may have to replace them with other characters, and then str_replace them back to what they should be, parse them out there, and then redirect to the new page.
<How would I go about that? That is exactly what I am trying to do.>
Perhaps something like this would work. First, the clickable URL in your HTML could look like this:
http://www.main.com/redirect.php?
NewPage=http://new.page.com/new.phpQUESTIONForeColor=RedAMPERSANDBackColor=Blue
And then the code in redirect.php to translate it might look like this:
<?php
$NewURL = str_replace("QUESTION", "?", str_replace("AMPERSAND", "&", $NewPage));
header("Location: $NewURL");
?>
This is untested, but you probably get the idea.
[edited by: jatar_k at 4:54 pm (utc) on Nov. 6, 2003]
[edit reason] broke url to fix sidescroll [/edit]
First, urlencode() the link before it's sent into the redirect script.
$url = urlencode('http://new.example.com/new.php?ForeColor=Red&BackColor=Blue');
$redir_url = 'http://www.example.com/redirect.php?NewPage=' . $url;
...
Then, urldecode() the link within redirect script.
$NewPage = urldecode($NewPage);
header("Location: $NewPage");
I tried the str_replace replace option and it gave the exact same results, anything after an ampersand was removed.
Any other ideas? I can't mess with the code on the page where the link will be displayed, I'd like to solve this one entirely within the redirect page if possible.
<Any other ideas? I can't mess with the code on the page where the link will be displayed, I'd like to solve this one entirely within the redirect page if possible.>
The other thing you can try is using the QUERY_STRING environment variable and see what it says. If it includes your entire query string, then you could try parsing it for your redirect.
It also may be a browser issue. If that's the case, you probably won't be able to get around it.
I think your whole url is still there just in more then one variable now. You've got to traverse the array to get everthing back. Try this:
$link = $HTTP_GET_VARS['link'];
reset ($HTTP_GET_VARS);
while (list ($key, $val) = each ($HTTP_GET_VARS)) {
if ($key!= "link")
$morevars = $morevars . "&" . $key . "=" . $val;
}$link = $url . $morevars
header("Location: $link");
Here's the functional code, now I'll see if it works with urls that have quotation marks in it.
<?php
/*
* Usage: index?link=http://www.etc......
*
*/
if($link == "")
die("somesite.com Hamster :: You must give me something to redirect");
$url = $HTTP_GET_VARS['link'];
reset ($HTTP_GET_VARS);
while (list ($key, $val) = each ($HTTP_GET_VARS)) {
if ($key!= "link")
$morevars = $morevars . "&" . $key . "=" . $val;
}
$link = $url . $morevars;
header("Location: $link");
?>
[edited by: jatar_k at 4:34 pm (utc) on Nov. 6, 2003]
[edit reason] generalized url [/edit]
1.) doublicate varname
the script you just posted won't work on a redirect to a page with a query variable named link. let's face it:
destination:
http://domain/index?id=1&link=about you will link to your redirect script that way:
http://mysite/redirect.php?link=http://domain/index?id=1&link=about because php does not handle duplicate varnames in query strings, redirect.php would redirect to (php takes the last value of link!):
about some browsers can take this kind of redirect information then as a relative link, leading these browser to navigate to:
http://mysite/about other browser will try to simple open the address:
about about:info in your browsers adressbar ;). 2.) nonstandard urls
and that's probably more important.
It needs to be able to support all URL types on the web.was stated in msg #1. writing a http url like:
http://mysite/redirect.php?link=http://domain/index?x=1&y=2 as said above multiple times, i strongly recommend to encode the values of the querystring the right way. php has a function built in for this task: url_encode [php.net].
if you can't use url_encode on the one side, do it by hand translating the link-value in the following manner:
Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the RFC1738 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs. This function is convenient when encoding a string to be used in a query part of an URL, as a convenient way to pass variables to the next page.
redirect.php. -hakre
i strongly recommend not to use the suggested ... as said above multiple times, i strongly recommend to encode the values
I can tell you fell strongly about this. I like a guy who's passionate about his code ;-)
I think we all agree that url_encode is the best way but as cdkrg has said he doesn't want to modify the code on that page.
I want to avoid doing any coding on the page that generates the link, it's on some software that is updated regularly and would make modifying it cumbersome.
So as I see it we either
a) Help him on the page that generates the links to make modifying it less cumbersome.
b) Use the suggested method. By all means check the url's validity and kick out an error in the off chance it has duplicate variables.
so then i would suggest a little mod-rewrite magik here to encapsulate the requested links to redirect. some url like this might be pleasureous?:
http://site/redir/http://new.page.com/new.php?ForeColor=Red&BackColor=Blue for me, that seems to be standard conform and everything needed can be put to a redir.php by modrewrite with ease. just a thought.