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


Groups > comp.lang.php > #7385

Re: Header and if statement

Message-ID <1727436.2xZtzPvNYR@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-10-29 14:54 +0100
Subject Re: Header and if statement
Newsgroups comp.lang.php
References <508e4026$1@news.synserver.de>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


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.)

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web