Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7381 > unrolled thread

Header and if statement

Started byBo Wisén <bowisen@telia.com>
First post2012-10-29 09:36 +0100
Last post2013-01-02 09:26 +0200
Articles 6 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  Header and if statement Bo Wisén <bowisen@telia.com> - 2012-10-29 09:36 +0100
    Re: Header and if statement Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-29 08:47 +0000
    Re: Header and if statement Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-29 14:54 +0100
    Re: Header and if statement Richard Yates <richard@yatesguitar.com> - 2012-12-26 09:29 -0800
      Re: Header and if statement "Peter H. Coffin" <hellsop@ninehells.com> - 2012-12-26 12:44 -0600
    Re: Header and if statement Simon <bad@example.com> - 2013-01-02 09:26 +0200

#7381 — Header and if statement

FromBo Wisén <bowisen@telia.com>
Date2012-10-29 09:36 +0100
SubjectHeader and if statement
Message-ID<508e4026$1@news.synserver.de>
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

[toc] | [next] | [standalone]


#7382

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-10-29 08:47 +0000
Message-ID<k6lfrf$vth$1@dont-email.me>
In reply to#7381
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

[toc] | [prev] | [next] | [standalone]


#7385

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-10-29 14:54 +0100
Message-ID<1727436.2xZtzPvNYR@PointedEars.de>
In reply to#7381
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.)

[toc] | [prev] | [next] | [standalone]


#7949

FromRichard Yates <richard@yatesguitar.com>
Date2012-12-26 09:29 -0800
Message-ID<7tcmd8hq7misu0lucvehtpfkh8d0f5upco@4ax.com>
In reply to#7381
On Mon, 29 Oct 2012 09:36:53 +0100, Bo Wisén <bowisen@telia.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.

[toc] | [prev] | [next] | [standalone]


#7951

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2012-12-26 12:44 -0600
Message-ID<slrnkdmhc7.4ja.hellsop@nibelheim.ninehells.com>
In reply to#7949
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@telia.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

[toc] | [prev] | [next] | [standalone]


#7988

FromSimon <bad@example.com>
Date2013-01-02 09:26 +0200
Message-ID<aki5oqFjohtU1@mid.individual.net>
In reply to#7381
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web