header php function challenge

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vidya
Forum Newbie
Posts: 6
Joined: Wed Mar 01, 2006 12:46 pm

header php function challenge

Post by vidya »

I'd like to use the header php function
to redirect to any given url.

But I ran into a problem. Check this:
My link.html code is simply this:

<?php
$link = $_GET['pg'];
header("Location: $link");

?>
<html>
<p><a href="<?=$link?>">Click Here</a></p>
</html>


Oughta work, eh?
But when I use this URL:
http://www.americanprogressaction.org/s ... ct=2026855

in a call like this:

http://www.webwisesage.com/link.html?pg ... ct=2026855

it gets a bad page address, whereas the page at the url is fine!

Any ideas?

...Vidya
http://www.webwisesage.com
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

In that code you are redirecting users before they can see that link.

Try changing the page extension to PHP, that might not be it, but it worked for me before.
vidya
Forum Newbie
Posts: 6
Joined: Wed Mar 01, 2006 12:46 pm

Not it

Post by vidya »

The site has an .htaccess set so that all html pages are treated as PHP pages.

And yes, I want to redirect users instantly.
Doesn't matter what code is below the header function.

Eventually, it will be statistic gathering.

Thanks

...Vidya
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the query part of the call should probably be encoded

rawurlencode()
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

oh, i dont think your .htaccess works right.

when i tested the code in PHP it worked.

when I tested in HTML it didnt
vidya
Forum Newbie
Posts: 6
Joined: Wed Mar 01, 2006 12:46 pm

Here's the atual reason it didn't work

Post by vidya »

I found out why it wasn't working.

When you have:
http://www.somedomain.com/link.html?pg= ... ct=2026855

where the link.html routine is doing a GET on the pg variable, the GET[pg] variable
is actually only http://www.americanprogressaction.org/s ... =klLWJcP7H

There's also a GET variable that is 1331575
and a GET[ct] that is 2026855

You can't just redirect to GET[pg]
You have to reassemble all the GET variables (using foreach)
and then redirect.

Simple when you see it!

....Vidya
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

using a urlencoded string wouldn't require any rigging like that.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

It worked for me. (when it was a php file)
Post Reply