/    Sign up×
Community /Pin to ProfileBookmark

if statement question

I don’t have the ability to test this at this time.

Does this look right?

If host equals bonds.mydomain.com then display bonds.php
If host equals stocks.mydomain.com then display stocks.php
If neither then display navbar_both.php

[code=php] <?php
IF ($_SERVER[‘HTTP_HOST’] == “bonds.mydomain.com”) :
include “bonds.php”;
ELSE IF ($_SERVER[‘HTTP_HOST’] == “stocks.mydomain.com”) :
include “stocks.php”;
ELSE :
include “navbar_both.php”;
ENDIF;
?>[/code]

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@dangergeekNov 07.2007 — I'm no php genius but i made a few small changes:

[CODE]
<?php
IF (($_SERVER['HTTP_HOST']) == "bonds.mydomain.com") {
include "bonds.php";
}
ELSE IF (($_SERVER['HTTP_HOST']) == "stocks.mydomain.com") {
include "stocks.php";
}
ELSE {
include "navbar_both.php";
}
ENDIF;
?>

[/CODE]
Copy linkTweet thisAlerts:
@NogDogNov 07.2007 — I'm no php genius but i made a few small changes:

[CODE]
<?php
IF (($_SERVER['HTTP_HOST']) == "bonds.mydomain.com") {
include "bonds.php";
}
ELSE IF (($_SERVER['HTTP_HOST']) == "stocks.mydomain.com") {
include "stocks.php";
}
ELSE {
include "navbar_both.php";
}
ENDIF;
?>

[/CODE]
[/QUOTE]

(1) The parentheses around the $_SERVER['HTTP_HOST'] variables are redundant and serve no purpose, though they will not inhibit the desired functionality. (2) If you are going to change to the if/else format using curly braces, then you must also get rid of the final ENDIF; statement. However, while the curly brace style is more commonly used, the syntax used in Shawn's original post is also valid. (See http://www.php.net/manual/en/control-structures.alternative-syntax.php.)
Copy linkTweet thisAlerts:
@dangergeekNov 07.2007 — Like a man who has had surgery to make his legs the same length, i stand corrected.
Copy linkTweet thisAlerts:
@comptech520authorNov 16.2007 — [B]Using this combination[/B]
[code=php]
<?php
IF ($_SERVER['HTTP_HOST'] == "photos.mydomain.com") :
include "http://www.mydomain.com/includes/subheader/justclients.php";
ELSE IF ($_SERVER['HTTP_HOST'] == "clients.mydomain.com") :
include "http://www.mydomain.com/includes/subheader/justphotos.php";
ELSE :
include "http://www.mydomain.com/includes/subheader/normal.php";
ENDIF;
?>
[/code]


[B]I get this error:[/B]

Parse error: syntax error, unexpected T_IF, expecting ':'
Copy linkTweet thisAlerts:
@NogDogNov 16.2007 — Make your [b]ELSE IF[/b] into one word: [b]ELSEIF[/b]. Otherwise you would have to do:
[code=php]
<?php
IF ($_SERVER['HTTP_HOST'] == "photos.mydomain.com") :
include "http://www.mydomain.com/includes/subheader/justclients.php";
ELSE: IF ($_SERVER['HTTP_HOST'] == "clients.mydomain.com") :
include "http://www.mydomain.com/includes/subheader/justphotos.php";
ELSE :
include "http://www.mydomain.com/includes/subheader/normal.php";
ENDIF;
ENDIF;
?> [/code]

This would get the same results, but is obviously clumsier to use.
Copy linkTweet thisAlerts:
@tgrk35Nov 16.2007 — Ok, you've got a few issues here that need to be corrected...

Use this:

[code=php]
<?php
if($_SERVER['HTTP_HOST'] == 'photos.mydomain.com'){
include 'justclients.php';
}
elseif($_SERVER['HTTP_HOST'] == 'clients.mydomain.com'){
include 'justphotos.php';
}
else{
include 'normal.php';
}
?>
[/code]


Changes:

  • 1. Don't capitalize functions...that's retarded ;p.

  • 2. elseif is actually one word...dumb, I know, but true.

  • 3. Use single quotes 99% of the time. Trust me on this one.


  • The end.

    Hope that helps.

    [B]Using this combination[/B]
    [code=php]
    <?php
    IF ($_SERVER['HTTP_HOST'] == "photos.mydomain.com") :
    include "http://www.mydomain.com/includes/subheader/justclients.php";
    ELSE IF ($_SERVER['HTTP_HOST'] == "clients.mydomain.com") :
    include "http://www.mydomain.com/includes/subheader/justphotos.php";
    ELSE :
    include "http://www.mydomain.com/includes/subheader/normal.php";
    ENDIF;
    ?>
    [/code]


    [B]I get this error:[/B]

    Parse error: syntax error, unexpected T_IF, expecting ':'[/QUOTE]
    Copy linkTweet thisAlerts:
    @comptech520authorNov 16.2007 — I followed your example and it isn't working. There are no errors, it just doesn't work.

    If the host is photos.mydomain.com

    it shows normal.php
    Copy linkTweet thisAlerts:
    @tgrk35Nov 16.2007 — damnit.

    well, echo out the $_SERVER['HTTP_HOST'] variable in each instance...see if php's logic is wrong or if YOUR logic is wrong ;p.
    Copy linkTweet thisAlerts:
    @comptech520authorNov 16.2007 — This post has been taken down.
    Copy linkTweet thisAlerts:
    @tgrk35Nov 16.2007 — You're not understanding what I'm getting at ;p.

    Since we're checking variables in our conditionals, we need to make sure those variables are actually what we think they are. Therefore, put this:

    [code=php]
    echo $_SERVER['HTTP_HOST'];
    [/code]


    ...underneath each 'include'.

    This way you will know exactly what the value of that variable is.
    Copy linkTweet thisAlerts:
    @comptech520authorNov 16.2007 — it comes up with www.esctonline.com as the host.

    If you goto http://photoweb.esctonline.com/server.php

    the host name shows photoweb
    Copy linkTweet thisAlerts:
    @tgrk35Nov 16.2007 — Change "mydomain" to your domain...
    Copy linkTweet thisAlerts:
    @comptech520authorNov 16.2007 — I did. but when i am pn the photoweb site it the text you had me echo shows up as esctonline.com. not photoweb.esctonline.com
    Copy linkTweet thisAlerts:
    @tgrk35Nov 16.2007 — Well, something's wrong if you can echo http_host and get the right value, but still have it not working...

    Paste the entire conditional you are using now.
    Copy linkTweet thisAlerts:
    @comptech520authorNov 16.2007 — on post 12 follow the server.php link. the http host there reads photoweb. when i echo what you told me to the http host reads www.esctonline.com when i am on the photoweb site. if the server.php file tells me one thing and the echo statement reads another, whats the problem?
    Copy linkTweet thisAlerts:
    @tgrk35Nov 17.2007 — Is server.php the file you're attempting to run the script on?
    Copy linkTweet thisAlerts:
    @comptech520authorNov 17.2007 — Server.php is on the photoweb site

    subheader.php is the file that has the php code we have been working on is in the main directory in the for esctonline.com
    Copy linkTweet thisAlerts:
    @tgrk35Nov 17.2007 — test your script on the same page as your print_r($_SERVER).
    Copy linkTweet thisAlerts:
    @comptech520authorNov 18.2007 — what do you mean by that?
    Copy linkTweet thisAlerts:
    @comptech520authorJan 23.2008 — I figured out what my problem is: the subheader.php file is in the main directory of my domain. When I echo the host on a index.php in a subdomain, it comes out with the mydomain.com because that is where the subjeader.php is located. Is there a way to get around this?
    ×

    Success!

    Help @comptech520 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...