FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Header and if statement
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Header and if statement [message #179448] Mon, 29 October 2012 08:36 Go to next message
Bo Wisn is currently offline  Bo Wisn
Messages: 1
Registered: October 2012
Karma: 0
Junior Member
Hi,

I want to redirect to one page if $typ=0 and another page if $typ=1. I'm
trying this code:

if ($typ == 0) {
header ("Location:page1.php");
} else {
header ("Location:page2.php");
}

But undepending on the value of $typ it redirects to page1.php. Why?

/ Bo
Re: Header and if statement [message #179449 is a reply to message #179448] Mon, 29 October 2012 08:47 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 29 Oct 2012 09:36:53 +0100, Bo Wisén wrote:

> Hi,
>
> I want to redirect to one page if $typ=0 and another page if $typ=1. I'm
> trying this code:
>
> if ($typ == 0) {
> header ("Location:page1.php");
> } else {
> header ("Location:page2.php");
> }
>
> But undepending on the value of $typ it redirects to page1.php. Why?

Is $typ an integer? Is $typ even defined? You haven't included the line
that assigns a value to $typ.

Rgds

Denis McMahon
Re: Header and if statement [message #179452 is a reply to message #179448] Mon, 29 October 2012 13:54 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Bo Wisén wrote:

> I want to redirect to one page if $typ=0 and another page if $typ=1. I'm
> trying this code:
>
> if ($typ == 0) {
> header ("Location:page1.php");
> } else {
> header ("Location:page2.php");
> }
>
> But undepending on the value of $typ it redirects to page1.php. Why?

There might be an explanation of that behavior when you post real,
executable code. With the code that you have posted, assuming that `$typ'
holds an integer or a string value as you suggested, the second header()
call will be executed if `$typ' does not hold 0 (zero) or "0". [1]

One problem is that you are using a type-juggling equality comparison
(`=='); you should probably be using a strict comparison instead: `==='. [2]

Another is that it is implementation-dependent whether an URI-reference
immediately following the colon will be accepted by a client; it is
specified in RFC 2616 that the header field value has to be an (absolute)
URI; it is customary (and better readable) that the colon is followed by a
space (leading white-space is not part of the field value). [3]

A third problem might be that there is output before the header() call,
because then no redirection should take place at all. (It is possible that
you are on page1.php already at this point.)

That said, you want to avoid redirections in your Web application. Most of
the time they are unnecessary and distracting to the user. [5]


PointedEars
___________
[1] <http://php.net/manual/en/control-structures.if.php>
[2] <http://php.net/manual/en/language.operators.php>
[3] <http://tools.ietf.org/html/rfc2616#section-14.30>
[4] <http://php.net/manual/en/function.header.php>
[5] <http://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_9/>
<https://developers.google.com/speed/docs/
best-practices/rtt#AvoidRedirects>
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
Re: Header and if statement [message #180017 is a reply to message #179448] Wed, 26 December 2012 17:29 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Mon, 29 Oct 2012 09:36:53 +0100, Bo Wisén <bowisen(at)telia(dot)com>
wrote:

> Hi,
>
> I want to redirect to one page if $typ=0 and another page if $typ=1. I'm
> trying this code:
>
> if ($typ == 0) {
> header ("Location:page1.php");
> } else {
> header ("Location:page2.php");
> }
>
> But undepending on the value of $typ it redirects to page1.php. Why?
>
> / Bo

Try adding exit; after each header statement and before the closing
curly brace.
Re: Header and if statement [message #180019 is a reply to message #180017] Wed, 26 December 2012 18:44 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Wed, 26 Dec 2012 09:29:34 -0800, Richard Yates wrote:
> On Mon, 29 Oct 2012 09:36:53 +0100, Bo Wis?n <bowisen(at)telia(dot)com>
> wrote:
>
>> Hi,
>>
>> I want to redirect to one page if $typ=0 and another page if $typ=1. I'm
>> trying this code:
>>
>> if ($typ == 0) {
>> header ("Location:page1.php");
>> } else {
>> header ("Location:page2.php");
>> }
>>
>> But undepending on the value of $typ it redirects to page1.php. Why?
>>
>> / Bo
>
> Try adding exit; after each header statement and before the closing
> curly brace.

Also make sure that $typ contains what you think it does, and make sure
that you're reporting errors. For example, unset and variables
holding false results evaluate == 0 (but do not === 0).

--
Every normal man must be tempted at times to spit upon his hands, hoist
the black flag, and begin slitting throats.
-- HL Mencken
Re: Header and if statement [message #180055 is a reply to message #179448] Wed, 02 January 2013 07:26 Go to previous message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
On 2012/10/29 10:36 AM, Bo Wisén wrote:

>
> if ($typ == 0) {
> header ("Location:page1.php");
> } else {
> header ("Location:page2.php");
> }

Where/how do you set the value $typ?
Is the code above inside a function?

Simon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Happy New Year
Next Topic: does the php is not popular?i am a phper?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 09 17:00:13 GMT 2024

Total time taken to generate the page: 0.03901 seconds