Re: [PHP] header("Location:...") fails

2010-01-14 Thread haliphax
On Wed, Jan 13, 2010 at 7:02 PM, Shawn McKenzie wrote:

> Robert Cummings wrote:
> > Just make your life easy and create a redirect() function that generates
> > the header instruction, makes a relative URL into an absolute URL and
> > does the exit call. Then you just need to do:
> >
> > redirect( 'target.php' );
> >
> > So much simpler :)
> >
> > Cheers,
> > Rob.
>
> Definitely!  Technically, header() with Location: should have an
> absolute URL, though it works without one most of the time.
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Also, when in doubt, I find it's usually a good idea to check the output
being sent with a more low-level tool, such as Fiddler, so that you can view
the raw values rather than leaving it up to your browser to interpret them.


// Todd


Re: [PHP] header("Location:...") fails

2010-01-13 Thread Shawn McKenzie
Robert Cummings wrote:
> Just make your life easy and create a redirect() function that generates
> the header instruction, makes a relative URL into an absolute URL and
> does the exit call. Then you just need to do:
> 
> redirect( 'target.php' );
> 
> So much simpler :)
> 
> Cheers,
> Rob.

Definitely!  Technically, header() with Location: should have an
absolute URL, though it works without one most of the time.

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] header("Location:...") fails

2010-01-13 Thread Robert Cummings

Paul M Foster wrote:

On Wed, Jan 13, 2010 at 11:39:18AM -0800, Richard S. Crawford wrote:


Here is a snippet of code that is going to be the death of me:


//  Create a new project
$projectcode = strtoupper(addslashes($_POST['projectcode']));   //  project
code

//  Make sure the project code is unique
if (!$existingproject = mysql_query("select * from pb_versions where
projectcode like '".strtoupper($projectcode)."'")) {
die ("Could not check for existing project code!".mysql_error());
}

$numprojects = mysql_num_rows($existingproject);

if ($numprojects > 0) {
$pid = mysql_result($existingproject,0,"versionID");
header("Location:managebudget.php?e=1&pid=$pid");
}


Now, even if $numprojects is 1, 2, 3, etc., the header() command is not
executed. Strangely, a header("Location") command later on in the script
*is* executed. I've output the value of $numprojects, so I know that it's
greater than 0, so the command
header("Location:managebudget.php?e=1&pid=$pid"); *should* be executed...
but it isn't. (Weirdly, if I put a die() command *after* this header()
command, it works... but it seems pathologically inelegant to do so.)

Obviously, I'm missing something incredibly basic. Can anyone help me figure
this out?


For one thing, I'd put a space after "Location:" in the header() call.
But I've found that this call will sometimes fail (or *look* like it
fails) unless you put something like exit(); after it. This terminates
execution and forces the script to transfer control as it should. Just
make it a habit to always include and exit(); call after your final
header() call.


Just make your life easy and create a redirect() function that generates 
the header instruction, makes a relative URL into an absolute URL and 
does the exit call. Then you just need to do:


redirect( 'target.php' );

So much simpler :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] header("Location:...") fails

2010-01-13 Thread Paul M Foster
On Wed, Jan 13, 2010 at 11:39:18AM -0800, Richard S. Crawford wrote:

> Here is a snippet of code that is going to be the death of me:
> 
> 
> //  Create a new project
> $projectcode = strtoupper(addslashes($_POST['projectcode']));   //  project
> code
> 
> //  Make sure the project code is unique
> if (!$existingproject = mysql_query("select * from pb_versions where
> projectcode like '".strtoupper($projectcode)."'")) {
> die ("Could not check for existing project code!".mysql_error());
> }
> 
> $numprojects = mysql_num_rows($existingproject);
> 
> if ($numprojects > 0) {
> $pid = mysql_result($existingproject,0,"versionID");
> header("Location:managebudget.php?e=1&pid=$pid");
> }
> 
> 
> Now, even if $numprojects is 1, 2, 3, etc., the header() command is not
> executed. Strangely, a header("Location") command later on in the script
> *is* executed. I've output the value of $numprojects, so I know that it's
> greater than 0, so the command
> header("Location:managebudget.php?e=1&pid=$pid"); *should* be executed...
> but it isn't. (Weirdly, if I put a die() command *after* this header()
> command, it works... but it seems pathologically inelegant to do so.)
> 
> Obviously, I'm missing something incredibly basic. Can anyone help me figure
> this out?

For one thing, I'd put a space after "Location:" in the header() call.
But I've found that this call will sometimes fail (or *look* like it
fails) unless you put something like exit(); after it. This terminates
execution and forces the script to transfer control as it should. Just
make it a habit to always include and exit(); call after your final
header() call.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] header("Location:...") fails

2010-01-13 Thread Andrew Ballard
On Wed, Jan 13, 2010 at 2:39 PM, Richard S. Crawford
 wrote:
> Now, even if $numprojects is 1, 2, 3, etc., the header() command is not
> executed. Strangely, a header("Location") command later on in the script
> *is* executed. I've output the value of $numprojects, so I know that it's
> greater than 0, so the command
> header("Location:managebudget.php?e=1&pid=$pid"); *should* be executed...
> but it isn't. (Weirdly, if I put a die() command *after* this header()
> command, it works... but it seems pathologically inelegant to do so.)
>
> Obviously, I'm missing something incredibly basic. Can anyone help me figure
> this out?

It isn't "pathologically inelegant" at all. All the header function
does is output the header; it does not stop script execution. If you
don't stop the script yourself, it will continue to execute. You
probably want to send a message right after the header call anyway,
just in case someone is using a browser that does not handle
redirection.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] header("Location:...") fails

2010-01-13 Thread Bruno Fajardo
2010/1/13 Richard S. Crawford 
>
> Here is a snippet of code that is going to be the death of me:
>
> 
> //  Create a new project
> $projectcode = strtoupper(addslashes($_POST['projectcode']));   //  project
> code
>
> //  Make sure the project code is unique
> if (!$existingproject = mysql_query("select * from pb_versions where
> projectcode like '".strtoupper($projectcode)."'")) {
>    die ("Could not check for existing project code!".mysql_error());
> }
>
> $numprojects = mysql_num_rows($existingproject);
>
> if ($numprojects > 0) {
>    $pid = mysql_result($existingproject,0,"versionID");
>    header("Location:managebudget.php?e=1&pid=$pid");
> }
> 
>
> Now, even if $numprojects is 1, 2, 3, etc., the header() command is not
> executed. Strangely, a header("Location") command later on in the script
> *is* executed. I've output the value of $numprojects, so I know that it's
> greater than 0, so the command
> header("Location:managebudget.php?e=1&pid=$pid"); *should* be executed...
> but it isn't. (Weirdly, if I put a die() command *after* this header()
> command, it works... but it seems pathologically inelegant to do so.)

There's nothing in wrong in putting a die command after the
header("Location"). In fact, it is common. The header() command by
itself don't imply in the send of the request. You can have many
header() commands in sequence, and the header will be sent only in the
end of the process.

Cheers,
Bruno.

>
> Obviously, I'm missing something incredibly basic. Can anyone help me figure
> this out?
>
>
> --
> Richard S. Crawford (rscrawf...@mossroot.com)
> http://www.mossroot.com
> Publisher and Editor in Chief, Daikaijuzine (http://www.daikaijuzine.com)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php