By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

header('Location:') jump ignored?

P: n/a
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.

I have some code that does some processing and then does a
header('Location: ...) jump to page A on success or falls through to
the jump to page B. This is the code:

if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) )
{
mysql_query( $q, $link ) ; // store the record
$ID = mysql_insert_id( $link ) ; // save the new id

if ( mysql_affected_rows($link) == 1 )
{
unlock_tables() ;
$_SESSION['ErrMsg'] = 'New ID is ' . $ID ;
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' Error: ignored jump' ;
}
else // the store failed
{ error handling }
}
// second chance (for debugging) to do the right thing
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' and it ignored it AGAIN!' ;

// and we shouldn't get here at all -- but we do!
header( 'Location: PageB.php ) ;
Unfortunately, as I know from the telltales I stuff into session, the
store works but the interpreter ignores both header calls that would
jump to A and finally jumps to B instead. Unless I'm more tired than
I'm aware, or more ignorant, this doesn't make sense.

Any straw gratefully clutched.

Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
If you display anything by the script first (such as echo 'hello
world<br>';) the header won't redirect after that.
Margaret MacDonald wrote:
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.

I have some code that does some processing and then does a
header('Location: ...) jump to page A on success or falls through to
the jump to page B. This is the code:

if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) )
{
mysql_query( $q, $link ) ; // store the record
$ID = mysql_insert_id( $link ) ; // save the new id

if ( mysql_affected_rows($link) == 1 )
{
unlock_tables() ;
$_SESSION['ErrMsg'] = 'New ID is ' . $ID ;
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' Error: ignored jump' ;
}
else // the store failed
{ error handling }
}
// second chance (for debugging) to do the right thing
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' and it ignored it AGAIN!' ;

// and we shouldn't get here at all -- but we do!
header( 'Location: PageB.php ) ;
Unfortunately, as I know from the telltales I stuff into session, the
store works but the interpreter ignores both header calls that would
jump to A and finally jumps to B instead. Unless I'm more tired than
I'm aware, or more ignorant, this doesn't make sense.

Any straw gratefully clutched.

Margaret


Jul 17 '05 #2

P: n/a
PagCal wrote:
If you display anything by the script first (such as echo 'hello
world<br>';) the header won't redirect after that.
Thanks for your quick response.

In this case, that's not a problem. Since I don't do any i/o it
actually does redirect. The problem is that it ignores the
redirection I expect!


Margaret MacDonald wrote:
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.

I have some code that does some processing and then does a
header('Location: ...) jump to page A on success or falls through to
the jump to page B. This is the code:

if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) )
{
mysql_query( $q, $link ) ; // store the record
$ID = mysql_insert_id( $link ) ; // save the new id

if ( mysql_affected_rows($link) == 1 )
{
unlock_tables() ;
$_SESSION['ErrMsg'] = 'New ID is ' . $ID ;
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' Error: ignored jump' ;
}
else // the store failed
{ error handling }
}
// second chance (for debugging) to do the right thing
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' and it ignored it AGAIN!' ;

// and we shouldn't get here at all -- but we do!
header( 'Location: PageB.php ) ;
Unfortunately, as I know from the telltales I stuff into session, the
store works but the interpreter ignores both header calls that would
jump to A and finally jumps to B instead. Unless I'm more tired than
I'm aware, or more ignorant, this doesn't make sense.

Any straw gratefully clutched.

Margaret


--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #3

P: n/a
On Tue, 03 Aug 2004 12:25:04 GMT, Margaret MacDonald
<sc**********@att.not.invalid> wrote:
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.

I have some code that does some processing and then does a
header('Location: ...) jump to page A on success or falls through to
the jump to page B. This is the code:

if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) )
{
mysql_query( $q, $link ) ; // store the record
$ID = mysql_insert_id( $link ) ; // save the new id

if ( mysql_affected_rows($link) == 1 )
{
unlock_tables() ;
$_SESSION['ErrMsg'] = 'New ID is ' . $ID ;
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' Error: ignored jump' ;
}
else // the store failed
{ error handling }
}
// second chance (for debugging) to do the right thing
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' and it ignored it AGAIN!' ;

// and we shouldn't get here at all -- but we do!
header( 'Location: PageB.php ) ;
Unfortunately, as I know from the telltales I stuff into session, the
store works but the interpreter ignores both header calls that would
jump to A and finally jumps to B instead. Unless I'm more tired than
I'm aware, or more ignorant, this doesn't make sense.


From the manual entry for header():

<quote>
The optional replace parameter indicates whether the header should
replace a previous similar header, or add a second header of the same
type. By default it will replace, but if you pass in FALSE as the
second argument you can force multiple headers of the same type.
</quote>

This suggests that your last header() call to redirect to B is
replacing your previous redirects to A.

Put an exit() after your header() to force the script to terminate.

--
David ( @priz.co.uk )
Jul 17 '05 #4

P: n/a
Margaret MacDonald wrote:
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.
Actually yes, that is what is happening.
I have some code that does some processing and then does a
header('Location: ...) jump to page A on success or falls through to
the jump to page B. This is the code:
Analyzing a few snippets of your code:
if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) )
{
mysql_query( $q, $link ) ; // store the record
$ID = mysql_insert_id( $link ) ; // save the new id

if ( mysql_affected_rows($link) == 1 )
{
unlock_tables() ;
$_SESSION['ErrMsg'] = 'New ID is ' . $ID ;
header( 'Location: PageA.php' ) ;
Here you set the header to PageA.php. It is set correctly as it should.
However, since you dont tell the script to exit(), the script continues
running.
$_SESSION['ErrMsg'] .= ' Error: ignored jump' ;
}
else // the store failed
{ error handling }
}
// second chance (for debugging) to do the right thing
header( 'Location: PageA.php' ) ;
Now you set the same header again. still, the script keeps running.
$_SESSION['ErrMsg'] .= ' and it ignored it AGAIN!' ;

// and we shouldn't get here at all -- but we do!
header( 'Location: PageB.php ) ;


Now you set the same header third time. By default setting the same
header again replaces the header, as it should. There is a way to force
php to send the same header more than once, although that is usually not
very smart, especially with the location-header.

Now that the script execution ends, the headers are sent, and the
location header points to the last one you set it to, PageB.php.

What you propably want to do is to add exit(); after each all three
header calls and you should be fine.

HTH

--
Suni

Jul 17 '05 #5

P: n/a
Just a sidenote:

The just released PHP5 contains a function headers_list() that would
have made debugging your problem very easy.

--
Suni

Jul 17 '05 #6

P: n/a
Juha Suni wrote:
Margaret MacDonald wrote:
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.
Actually yes, that is what is happening.
I have some code that does some processing and then does a
header('Location: ...) jump to page A on success or falls through to
the jump to page B. This is the code:


Analyzing a few snippets of your code:
if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) )
{
mysql_query( $q, $link ) ; // store the record
$ID = mysql_insert_id( $link ) ; // save the new id

if ( mysql_affected_rows($link) == 1 )
{
unlock_tables() ;
$_SESSION['ErrMsg'] = 'New ID is ' . $ID ;
header( 'Location: PageA.php' ) ;


Here you set the header to PageA.php. It is set correctly as it should.
However, since you dont tell the script to exit(), the script continues
running.


aaaAAAAAaaaaa! *THAT* was the piece I was missing. I thought (don't
ask me why) the header call with a location argument was the actual
jump.

As Mark Twain didn't quite say, it's not our ignorance but our
meta-ignorance that will get us, every time.

Thank you very much for deconfusing me.

$_SESSION['ErrMsg'] .= ' Error: ignored jump' ;
}
else // the store failed
{ error handling }
}
// second chance (for debugging) to do the right thing
header( 'Location: PageA.php' ) ;


Now you set the same header again. still, the script keeps running.
$_SESSION['ErrMsg'] .= ' and it ignored it AGAIN!' ;

// and we shouldn't get here at all -- but we do!
header( 'Location: PageB.php ) ;


Now you set the same header third time. By default setting the same
header again replaces the header, as it should. There is a way to force
php to send the same header more than once, although that is usually not
very smart, especially with the location-header.

Now that the script execution ends, the headers are sent, and the
location header points to the last one you set it to, PageB.php.

What you propably want to do is to add exit(); after each all three
header calls and you should be fine.

HTH


--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #7

P: n/a
David Mackenzie wrote:
On Tue, 03 Aug 2004 12:25:04 GMT, Margaret MacDonald
<sc**********@att.not.invalid> wrote:
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.

I have some code that does some processing and then does a
header('Location: ...) jump to page A on success or falls through to
the jump to page B. This is the code:

if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) )
{
mysql_query( $q, $link ) ; // store the record
$ID = mysql_insert_id( $link ) ; // save the new id

if ( mysql_affected_rows($link) == 1 )
{
unlock_tables() ;
$_SESSION['ErrMsg'] = 'New ID is ' . $ID ;
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' Error: ignored jump' ;
}
else // the store failed
{ error handling }
}
// second chance (for debugging) to do the right thing
header( 'Location: PageA.php' ) ;
$_SESSION['ErrMsg'] .= ' and it ignored it AGAIN!' ;

// and we shouldn't get here at all -- but we do!
header( 'Location: PageB.php ) ;
Unfortunately, as I know from the telltales I stuff into session, the
store works but the interpreter ignores both header calls that would
jump to A and finally jumps to B instead. Unless I'm more tired than
I'm aware, or more ignorant, this doesn't make sense.


From the manual entry for header():

<quote>
The optional replace parameter indicates whether the header should
replace a previous similar header, or add a second header of the same
type. By default it will replace, but if you pass in FALSE as the
second argument you can force multiple headers of the same type.
</quote>

This suggests that your last header() call to redirect to B is
replacing your previous redirects to A.

Put an exit() after your header() to force the script to terminate.


Thanks, David. It's the 'put an exit() after your header()' part that
I was missing. I didn't understand that that call to header doesn't
terminate execution...and since the 20 other cases where I make a
similar call are working correctly purely by accident.... :-}

Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #8

P: n/a
.oO(Juha Suni)
Margaret MacDonald wrote:
header( 'Location: PageA.php' ) ;


Here you set the header to PageA.php. It is set correctly as it should.


Nope, it's _not_ set correctly (even if it's not the real problem in
this case). The location-header requires an absolute URL including the
scheme (http://) and hostname. Most browsers also accept the "short"
version, but it's broken and not standards-compliant.

Micha
Jul 17 '05 #9

P: n/a
Michael Fesser wrote:
.oO(Juha Suni)
Margaret MacDonald wrote:
header( 'Location: PageA.php' ) ;


Here you set the header to PageA.php. It is set correctly as it
should.


Nope, it's _not_ set correctly (even if it's not the real problem in
this case). The location-header requires an absolute URL including the
scheme (http://) and hostname. Most browsers also accept the "short"
version, but it's broken and not standards-compliant.


Yes indeed, I meant to mention that in my post too, but forgot. Thanks
for the correction. Many people tend to use relative urls for location
header, where they would propably be better of with a custom function or
use of an included config file that would specify the "root-url" and add
the specified relative url to that for the header call.

for example:

function relative_redirect($relurl) {
$rooturl = 'http://www.mysite.com.invalid/mydir/';
// possibly add a if file_exists()-check for local
// redirections for easier debugging and/or exception handling
header('Location: ' . $rooturl . $relurl);
exit();
}

--
Suni

Jul 17 '05 #10

P: n/a
for example:

function relative_redirect($relurl) {
$rooturl = 'http://www.mysite.com.invalid/mydir/';
// possibly add a if file_exists()-check for local
// redirections for easier debugging and/or exception handling
header('Location: ' . $rooturl . $relurl);
exit();
}


if you use php as a cgi with mod fastcgi you can do:

header( 'Location: /PageA.php' ) ;

and fastcgi will make an internal redirection to PageA.php, the client
browser won't see anything.

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.