Home »
Imported messages »
comp.lang.php »
Header and if statement
Re: Header and if statement [message #179452 is a reply to message #179448] |
Mon, 29 October 2012 13:54 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
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.)
|
|
|
Goto Forum:
Current Time: Sat Nov 09 16:36:22 GMT 2024
Total time taken to generate the page: 0.04304 seconds