header("Location:...") fails
header("Location:...") fails
am 13.01.2010 20:39:18 von rscrawford
--0016e64dd6d2513db8047d10ee9e
Content-Type: text/plain; charset=ISO-8859-1
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?
--
Richard S. Crawford (rscrawford@mossroot.com)
http://www.mossroot.com
Publisher and Editor in Chief, Daikaijuzine (http://www.daikaijuzine.com)
--0016e64dd6d2513db8047d10ee9e--
Re: header("Location:...") fails
am 13.01.2010 20:48:09 von Bruno Fajardo
2010/1/13 Richard S. Crawford
>
> Here is a snippet of code that is going to be the death of me:
>
> ------------------------------------------------------------
> // =A0Create a new project
> $projectcode =3D strtoupper(addslashes($_POST['projectcode'])); =A0 // =
=A0project
> code
>
> // =A0Make sure the project code is unique
> if (!$existingproject =3D mysql_query("select * from pb_versions where
> projectcode like '".strtoupper($projectcode)."'")) {
> =A0 =A0die ("Could not check for existing project code!
".mysql_erro=
r());
> }
>
> $numprojects =3D mysql_num_rows($existingproject);
>
> if ($numprojects > 0) {
> =A0 =A0$pid =3D mysql_result($existingproject,0,"versionID");
> =A0 =A0header("Location:managebudget.php?e=3D1&pid=3D$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=3D1&pid=3D$pid"); *should* be execute=
d...
> 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 fig=
ure
> this out?
>
>
> --
> Richard S. Crawford (rscrawford@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
Re: header("Location:...") fails
am 13.01.2010 20:51:04 von 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: header("Location:...") fails
am 13.01.2010 21:26:17 von 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: header("Location:...") fails
am 13.01.2010 22:02:59 von 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' );
Sooooooooo 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: header("Location:...") fails
am 14.01.2010 02:02:25 von 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' );
>
> Sooooooooo 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: header("Location:...") fails
am 14.01.2010 15:22:08 von haliphax
--0016e6d99acebe7a3e047d209c6e
Content-Type: text/plain; charset=UTF-8
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' );
> >
> > Sooooooooo 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
--0016e6d99acebe7a3e047d209c6e--