Forum Moderators: coopster
$request = $_REQUEST['target'];
header("location: $request");
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
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
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="">here</a></body>
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;
}
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
...
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
$g = $_GET['g'];
if ($g == '') {
$target = 'http://www.google.co.uk';
$request = $_REQUEST['target'];
header("Location: $request");
exit;
}
-
$_REQUESTis 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
$targethere. 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.