Forum Moderators: coopster

Message Too Old, No Replies

I just want to redirect to another script!

         

ocelot

8:24 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



but how?!

header("Location: $_REQUEST[target]");

doesn't seem to work

dreamcatcher

8:35 pm on Nov 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does $_REQUEST['target'] have a value? Try echoing it before you call the header function.

echo $_REQUEST['target'];
exit;
header("Location: $_REQUEST['target']");

Notice I used apostrophes. It should work with or without.

ocelot

8:39 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



hmmm.. well I did

$target = "www.msn.com";

echo $_REQUEST['target'];
exit;
header("Location: $_REQUEST['target']");

and it said

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

mbugg

9:53 pm on Nov 7, 2004 (gmt 0)



Well, this seems to work

$request = $_REQUEST['target'];
header("location: $request");

ocelot

10:39 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



well when I do

$request = $_REQUEST['www.google.com'];
header("location: $request");

(or any site..I'm just sticking in msn and google to test it out)

it gives me a blank page

ergophobe

11:06 pm on Nov 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's a raw header. You need to specify a protocol I beleive (i.e. http or ftp etc)

try

$site = 'http://example.com';

header("Location: " . $site);

Tom

bubone2

11:43 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



dreamcatcher,

Are you sure that the first example you gave will work, where you told ocelot to echo it before sending the header.

If I remember right, you cannot post anything (even whitespace or <html> tag) before you start tampering with the header. You can do any kind of php that you want before the header statement as long as you don't print anything to the page.

And so that is why ocelot got that error:

"Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING "

Personally I find that kind of annoying, especially when trying to set cookies.

Josh

ergophobe

1:24 am on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Josh,

Dreamcatcher was just showing him how to debug the script. His example does not send a header as it exits first.

You are quite right that if you removed the exit() and tried to echo then send a header of any sort whatsoever, you would get the "headers already sent" error.

The error is because the PHP parse can't figure out what to do with this line

header("Location: $_REQUEST['target']");

You need to tell it where to delimit the variable. In other words, it thinks a variable named $_REQUEST should evaluate to a string, but it doesn't. You need to use curly braces or concatenation to get it to work as in

header("Location: {$_REQUEST['target']}");
header("Location: " . $_REQUEST['target']);

Also, this

$target = 'www.msn.com';
echo $_REQUEST['target'];

echoes the value of $_REQUEST['target'], sure, but what is it? Nobody knows, so it's probably '' (that is, an empty string). If you had register_globals on (and you shouldn't right?) and you received $_REQUEST['target'] as, say, post data, then $target would be defined. It doesn't work the other way around, though and, ideally, it shouldn't work in either direction.

Tom

Tom

dreamcatcher

2:22 am on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Tom. Yes, I used exit to terminate the script before the header function. As Tom mentioned, I was just helping with a little debugging.

:)

bubone2

10:49 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



ok, sorry guys. Guess I didn't realize what that would do.

I just knew that I had seen a similar error before, and it was related to me outputting some whitespace that I didn't realize.

My apologies to dreamcatcher. ;-)

Josh

Hester

2:52 pm on Nov 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would like to cause a script to move to another web page on error. I have tried the examples above but I get this code come up:

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="">here</a></body>

Where does that come from!?

Here is my exact PHP I'm using. It may well contain errors so feel free to point them out.


$g = $_GET['g'];
if ($g == '') {
$target = 'http://www.google.co.uk';
$request = $_REQUEST['target'];
header("location: $request");
exit;
}

ergophobe

6:46 pm on Nov 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




My apologies to dreamcatcher. ;-)

Not to answer for dreamcatcher yet again, but I don't think apologies are in order - your question helped clarify the matter, which is a good thing. That's why we have a discussion group.

Hester, I have no idea why that's happening. Is the URL for the page in question at the google.co.uk domain or is the redirect failing?

Tom

Hester

9:45 am on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The redirect is failing. The address bar shows the same page. Oddly, the link given just takes you to the same page. I'm guessing this is some built-in PHP error code.

Does anyone have a script that will definitely work? I'm using PHP 4 on Windows IIS.

baertyp

10:32 am on Nov 11, 2004 (gmt 0)

10+ Year Member



Why dont you just do


...
set value of $target somewhere, like
$target = 'http://www.google.co.uk';
...
$g = $_GET['g'];
if (!$g) {
header("Location: $target");
exit;
}
...

as this is what the value of the "Location" header is supposed to look like. It's a suggestion for the client to look for the requested information at another URI. Can't quite understand why you keep fiddling around with the $_REQUEST superglobal. This array is populated by PHP and, except for $_SESSION, all of the superglobals are more sort of "readonly". Well, I might be wrong here, but that's the way I treat them anyway.

Regards
Markus

mincklerstraat

10:43 am on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Hester, it does look like there might be things here that won't work as intended.

$g = $_GET['g'];
if ($g == '') {
$target = 'http://www.google.co.uk';
$request = $_REQUEST['target'];
header("Location: $request");
exit;
}

-

$_REQUEST
is an array consisting of the various parameters that are sent as part of the request - get, post, cookie - so
$_REQUEST['target']
would be set to anything that you sent as a post get or cookie item with the name "target" - I'm guessing, you've confused this with something else. Otherwise, there's no reason to be setting
$target
here. I've also changed your code a bit regarding
$_GET['g']
. If we change you code:


/*$g = $_GET['g'];
if ($g == '') commented out*/
if(empty($_GET['g'])){
$target = 'http://www.google.co.uk';
/* $request = $_REQUEST['target']; commented out */
header("location: $target");
exit;
}

Hope this helps solve your problem.

Hester

4:04 pm on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It worked! Thanks guys! Now I can hopefully pass a variable to the next page so it echoes a specific error message.

ocelot

1:45 am on Nov 18, 2004 (gmt 0)

10+ Year Member



what about if I want to pass variables to the page that I redirect to, WITHOUT having them show up in the url?

how can I do that?

Hester

9:39 am on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could always encode the variable first.