Header and if statement

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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

On Mon, 29 Oct 2012 09:36:53 +0100, Bo Wisén wrote:

Quoted text here. Click to load it

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


Denis McMahon

Re: Header and if statement

Bo Wisén wrote:

Quoted text here. Click to load it

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]

[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 /
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


Quoted text here. Click to load it

Try adding exit; after each header statement and before the closing
curly brace.

Re: Header and if statement

On Wed, 26 Dec 2012 09:29:34 -0800, Richard Yates wrote:
Quoted text here. Click to load it

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

On 2012/10/29 10:36 AM, Bo Wisn wrote:

Quoted text here. Click to load it

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


Site Timeline