header("Location: page.php") not redirecting

This is a discussion on header("Location: page.php") not redirecting within the PHP General forums, part of the PHP Programming Forums category; Hi all, I have this in a template: $redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar"; header(&...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-01-2003
Rogue
 
Posts: n/a
Default header("Location: page.php") not redirecting

Hi all,

I have this in a template:

$redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
header("Location: $redirect");

Only, nothing happens when this code is executed. Nothing. It gets
completely ignored.

I even tried:

$redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
echo $redirect;
header("Location: $redirect");

to try and throw an error, but all that happens is the value of
$redirect gets sent to the screen and then rest of the page is
displayed. I know the code block is getting executed, but I have no
idea WHY the header() function is not doing its job.

Any thoughts? Please CC me on the post since I am on the digest.

Thanks
Reply With Quote
  #2 (permalink)  
Old 12-01-2003
Chris Shiflett
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

--- rogue <rogue@mail.rightcode.net> wrote:
> $redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
> header("Location: $redirect");
>
> Only, nothing happens when this code is executed.


I would try two things, in this order:

1. Use a full URL. A Location header is defined in the specification as
taking a full URL as an argument. You are using a relative one, which is
incorrect.

2. Replace your header call with an echo. Basically, rather than
header("Location: $redirect") use echo "Location: $redirect" instead. This
might reveal typos or other problems that are more difficult to inspect
when in the HTTP headers instead of the content.

Hope that helps.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security Handbook
Coming mid-2004
HTTP Developer's Handbook
http://httphandbook.org/
Reply With Quote
  #3 (permalink)  
Old 12-01-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

* Thus wrote rogue (rogue@mail.rightcode.net):
>
> $redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
> header("Location: $redirect");
>
> Only, nothing happens when this code is executed. Nothing. It gets
> completely ignored.
>
> I even tried:
>
> $redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
> echo $redirect;
> header("Location: $redirect");


You're error_reporting is too low, set it to something like:
error_reporting(E_ALL);

before the header() call

Curt
--
If eval() is the answer, you're almost certainly asking the
wrong question. -- Rasmus Lerdorf, BDFL of PHP
Reply With Quote
  #4 (permalink)  
Old 12-01-2003
Rogue
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

Okay. I reworked the code a bit, but still no go.

Here is what I have:

$redirect = "http://" . $_SERVER['HTTP_HOST'] .
"/cm/clients/viewclient.php?id={$_REQUEST['id']}&foo=bar";
error_reporting(E_ALL);
echo "Location: $redirect";
header("Location: $redirect");

I get NO errors at all :(
It was my understanding that the only thing I needed to worry about
breaking this, would be if there was some output before the header().
There is not, but there are some variables getting set etc.

Anyone else think of anything?

Thanks

On Dec 1, 2003, at 4:48 PM, Chris Shiflett wrote:

> --- rogue <rogue@mail.rightcode.net> wrote:
>> $redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
>> header("Location: $redirect");
>>
>> Only, nothing happens when this code is executed.

>
> I would try two things, in this order:
>
> 1. Use a full URL. A Location header is defined in the specification as
> taking a full URL as an argument. You are using a relative one, which
> is
> incorrect.
>
> 2. Replace your header call with an echo. Basically, rather than
> header("Location: $redirect") use echo "Location: $redirect" instead.
> This
> might reveal typos or other problems that are more difficult to inspect
> when in the HTTP headers instead of the content.
>
> Hope that helps.
>
> Chris
>
> =====
> Chris Shiflett - http://shiflett.org/
>
> PHP Security Handbook
> Coming mid-2004
> HTTP Developer's Handbook
> http://httphandbook.org/

Reply With Quote
  #5 (permalink)  
Old 12-01-2003
Chris Shiflett
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

--- rogue <rogue@mail.rightcode.net> wrote:
> $redirect = "http://" . $_SERVER['HTTP_HOST'] .
> "/cm/clients/viewclient.php?id={$_REQUEST['id']}&foo=bar";
> error_reporting(E_ALL);
> echo "Location: $redirect";
> header("Location: $redirect");


I meant to try using a full URL first, then use echo if it still doesn't
work. :-) This shouldn't work as it is, because you have output prior to
your header call.

But, since you already tried this, what did the echo output? Did it look
correct? Comment that out, and see what happens.

Hope that helps.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security Handbook
Coming mid-2004
HTTP Developer's Handbook
http://httphandbook.org/
Reply With Quote
  #6 (permalink)  
Old 12-01-2003
Tom Rogers
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

Hi,

Tuesday, December 2, 2003, 7:17:58 AM, you wrote:
r> Hi all,

r> I have this in a template:

r> $redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
r> header("Location: $redirect");

r> Only, nothing happens when this code is executed. Nothing. It gets
r> completely ignored.

r> I even tried:

r> $redirect = "viewclient.php?id={$_REQUEST['id']}&foo=bar";
r> echo $redirect;
r> header("Location: $redirect");

r> to try and throw an error, but all that happens is the value of
r> $redirect gets sent to the screen and then rest of the page is
r> displayed. I know the code block is getting executed, but I have no
r> idea WHY the header() function is not doing its job.

r> Any thoughts? Please CC me on the post since I am on the digest.

r> Thanks


Try adding exit; after the header call.

--
regards,
Tom
Reply With Quote
  #7 (permalink)  
Old 12-01-2003
Rogue
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

hi again,

echo looks fine (no error in the URL). I am stumped. Is there anything
besides output to the screen before header() that breaks it?

thanks

On Dec 1, 2003, at 5:13 PM, Chris Shiflett wrote:

> --- rogue <rogue@mail.rightcode.net> wrote:
>> $redirect = "http://" . $_SERVER['HTTP_HOST'] .
>> "/cm/clients/viewclient.php?id={$_REQUEST['id']}&foo=bar";
>> error_reporting(E_ALL);
>> echo "Location: $redirect";
>> header("Location: $redirect");

>
> I meant to try using a full URL first, then use echo if it still
> doesn't
> work. :-) This shouldn't work as it is, because you have output prior
> to
> your header call.
>
> But, since you already tried this, what did the echo output? Did it
> look
> correct? Comment that out, and see what happens.
>
> Hope that helps.
>
> Chris
>
> =====
> Chris Shiflett - http://shiflett.org/
>
> PHP Security Handbook
> Coming mid-2004
> HTTP Developer's Handbook
> http://httphandbook.org/

Reply With Quote
  #8 (permalink)  
Old 12-01-2003
Chris Shiflett
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

--- rogue <rogue@mail.rightcode.net> wrote:
> echo looks fine (no error in the URL). I am stumped. Is there
> anything besides output to the screen before header() that breaks
> it?


I'm sure there are lots of things, but I can't think of why your code
would not work. You did remove the echo before trying again, right? Can
you show us the exact output of the echo call?

A more reliable way to see what is going on is to view the actual HTTP
response your script is generating. Do you have a way to examine this?
There is a Mozilla plugin that can show you the HTTP, and there is also
things such as ethereal which can sniff your network traffic. This would
show you whether the proper Location header is actually being sent in the
response.

I suspect it is not, as I'm not aware of any browser that doesn't handle
this header correctly.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security Handbook
Coming mid-2004
HTTP Developer's Handbook
http://httphandbook.org/
Reply With Quote
  #9 (permalink)  
Old 12-01-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] header("Location: page.php") not redirecting

* Thus wrote rogue (rogue@mail.rightcode.net):
> Okay. I reworked the code a bit, but still no go.
>
> Here is what I have:
>
> $redirect = "http://" . $_SERVER['HTTP_HOST'] .
> "/cm/clients/viewclient.php?id={$_REQUEST['id']}&foo=bar";
> error_reporting(E_ALL);
> echo "Location: $redirect";
> header("Location: $redirect");


hmm...
- add a ini_set('display_errors', true);
- check your logs, apache and php

That code is throwing an warning somewhere.

Curt
--
If eval() is the answer, you're almost certainly asking the
wrong question. -- Rasmus Lerdorf, BDFL of PHP
Reply With Quote
  #10 (permalink)  
Old 12-02-2003
Richard Davey
 
Posts: n/a
Default Re[2]: [PHP] header("Location: page.php") not redirecting

Hello rogue,

Monday, December 1, 2003, 10:39:02 PM, you wrote:

r> echo looks fine (no error in the URL). I am stumped. Is there anything
r> besides output to the screen before header() that breaks it?

Other than browser incompatibility, not usually.

You can use the following to determine if a header has been sent
without you perhaps realising it (an extra space before a PHP tag
perhaps)

<?
if (!headers_sent()) {
header ('Location: http://www.google.com/');
exit;
}
?>

Ideally you should exit after the header is finished.

--
Best regards,
Richard mailto:rich@launchcode.co.uk
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:34 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0