Forum Moderators: coopster

Message Too Old, No Replies

Simple redirection

redirection in php

         

cdkrg

9:13 am on Oct 28, 2003 (gmt 0)

10+ Year Member



Ok, I want to write a very simple redirection script in PHP. I will be redirecting to pages off of my website.

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.

Birdman

9:31 am on Oct 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use url_encode() [php.net] on the link before calling the script...I think? Give it a try. It should convert any non-alphanumeric characters.

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;
?>

cdkrg

11:40 am on Oct 29, 2003 (gmt 0)

10+ Year Member



Doesn't seem to be working. I know it's something simple which is one reason it's frustrating.

coopster

1:13 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Should work as is unless you have some funky characters (especially double-quotes) in your variables, in which you may want to urlencode them as suggested by Birdman.

I did notice, however, that your keys have no values. Are you sure your don't what something like:
[somesite.com...]

cdkrg

8:00 am on Oct 30, 2003 (gmt 0)

10+ Year Member



It hasn't made a difference. If an & is found in the link it truncates from that point on.

NickCoons

8:47 am on Oct 30, 2003 (gmt 0)

10+ Year Member



cdkrg,

<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.

cdkrg

5:05 am on Oct 31, 2003 (gmt 0)

10+ Year Member



How would I go about that? That is exactly what I am trying to do.

NickCoons

7:09 am on Oct 31, 2003 (gmt 0)

10+ Year Member



cdkrg,

<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]

ams_david

7:48 am on Oct 31, 2003 (gmt 0)

10+ Year Member



maybe....

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");

NickCoons

8:31 am on Oct 31, 2003 (gmt 0)

10+ Year Member



ams_david,

That would probably be simpler and cleaner :-).

cdkrg

9:03 pm on Oct 31, 2003 (gmt 0)

10+ Year Member



Well, the thing is, 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.

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.

NickCoons

1:37 am on Nov 1, 2003 (gmt 0)

10+ Year Member



cdkrg,

<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.

Timotheos

7:14 am on Nov 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to change the title of this thread to "Simple redirection but I've got one hand tied behind my back" :-)

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");


There's probably a more elegant way to do it but I think it's functional

cdkrg

9:18 am on Nov 6, 2003 (gmt 0)

10+ Year Member



I do indeed seem to have a hand tied here. I can't get that last snippet to work either.

I wonder how Brett does it here on WebmasterWorld.

cdkrg

9:25 am on Nov 6, 2003 (gmt 0)

10+ Year Member



Timotheos BTW, for some reason the snippet you posted is removing all of the url before the ampersand.

This is driving me nuts.

cdkrg

9:38 am on Nov 6, 2003 (gmt 0)

10+ Year Member



Ok, I got the snippet to work, there were just a few minor errors in it.

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]

hakre

10:22 am on Nov 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i strongly recommend not to use the suggested method because of 2 points:

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

guess what's happening then. for example try
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

is not a valid http url at all. your browser may get around with this, but - this thread is about it - it does not. only because you want your browser to do so it won't do it ;).

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.

this is taken out of the php docs and as you can see this is just about the case this thread is about. it's a convenient way to pass variables to the next page, in this case the
redirect.php
.

-hakre

Timotheos

4:42 pm on Nov 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

hakre

7:35 am on Nov 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well, timo, right. nevertheless i think it's good to know about the drawbacks, cauz he can run into serious trouble with something that does not display valid urls.

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.

cdkrg

8:36 am on Nov 19, 2003 (gmt 0)

10+ Year Member



The solution has worked fine so far. I will just change the link variable name to something unlikely to ever be seen in a URL.