PHP Operator Help

Why is it when I use the code below, the $WebHost is not equal to domain.com and the top portion of the code is used instead of the bottom?

<?php
$WebHost = "domain.com";
if ($_SERVER['SERVER_NAME'] != $WebHost)
{
$AbsolutePath = $_SERVER['DOCUMENT_ROOT']."/";
$SubDomainHomePath = $_SERVER['DOCUMENT_ROOT']."/subdomains/";
$BasePath = "http://www.$WebHost/";
$WebsiteURL = "http://www.$WebHost/";
}
else
{
$AbsolutePath = $_SERVER['DOCUMENT_ROOT']."/adasdasdasdasdasdasdasdasd";
$SubDomainHomePath = "/subdomains/";
$BasePath = "http://localhost/";
$WebsiteURL = "http://localhost/";
}
?>

Open in new window

LVL 3
Computer GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Try to use this:

$WebHost = "domain.com";
if (in_array($_SERVER['HTTP_HOST'], $WebHost){
...
0
Computer GuyAuthor Commented:
Hi, Looking to make my code work. That didn't.
0
Chris StanyonWebDevCommented:
var_dump the $_SERVER array and see what the SERVER_NAME is set to.

If your page is called at www.domain.com then the SERVER_NAME will be www.domain.com and your IF statement will return false!
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

GaryCommented:
Don't understand the logic of what you are doing but you are saying if it is not equal
SERVER_NAME will be the current server - what is $WebHost supposed to represent?
0
zappafan2k2Commented:
If you really want to be safe, force them both to either upper case or lower case, and use preg_match()
$WebHost = strtoupper('domain.com');
if (preg_match("/$WebHost/", strtoupper($_SERVER['HTTP_HOST']))) {
...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Use var_dump($_SERVER); to see what is in there.  If you post that output here, we can probably show  you some programming that can help you get what you want.  Also, sub-directories are the things to the rightmost end of the URL after the slash directory separator.  Sub-domains are like www and test in these URLs

http://www.twitter.com
http://test.twitter.com
0
Computer GuyAuthor Commented:
Hi,

I am trying to have a some code in a file that is included on all pages. When I work on my site offline, it will work and when I upload it it will work. Just because the paths are different between my testing server and my live server.

If WebHost = domain.com, ...there is some more code underneath that will change the paths I have hard coded in my site.
0
Ray PaseurCommented:
Please use var_dump($_SERVER);.  Run it on your live site and also on your testing server.  Please use "view source" then copy and post the output here in the code snippet.  Thanks, ~Ray
0
Chris StanyonWebDevCommented:
Do you call your website up with www.domain.com or just domain.com - it makes a difference!!

Have you followed any of the suggestions posted by Experts.

Have you ran var_dump($_SERVER) to see what's in there?
0
hieloCommented:
Why is it when I use the code below, the $WebHost is not equal to domain.com and the top portion of the code is used instead of the bottom?
Because that is exactly how you programmed it.  You probably meant:

if ($_SERVER['SERVER_NAME'] == $WebHost)
0
Mark BradyPrincipal Data EngineerCommented:
Here is how I always find the running path for my scripts. Once you run this code once you can set it in a constant and use that throughout you other code.

<?php
// Get current running directory
if (__FILE__ !== "__FILE__") {
	$path = dirname(__FILE__);
} else if (__DIR__ !== "__DIR__") {
	$path = __DIR__;
} else if (isset($_SERVER['SCRIPT_FILENAME'])) {
	$path = dirname($_SERVER['SCRIPT_FILENAME']);
}

$path = explode((strpos($path, '/') !== false ? '/' : '\\'), $path);

// Included files
if (!defined('ROOT') define ('ROOT', $path[0]);

?>

Open in new window


Now it is defined in a constant so all you need to do to include any other file is know which sub folder you have the file. For example using my code above if I wanted to include the file 'test.php' and that file was located in /scripts folder (so the url would be www.mywebsite.com/scripts/test.php) then I would do this.

include_once ROOT.'/scripts/test.php';

Now that file is included.

Get the idea?
0
Ray PaseurCommented:
@elvin66: Love it!  You might find a use for the context-aware PHP constant DIRECTORY_SEPARATOR which will always give you the correct choice of front or back slashes.  Cheers, ~Ray
0
Mark BradyPrincipal Data EngineerCommented:
Ahh yes Ray I actually use that now. The sample I posted was from an old script I just happened to have handy at work but thanks for the clarification.
0
Computer GuyAuthor Commented:
I see your point on www¿or no wwwhow about this. If server name = h1 h2 or h3 then....

H1 domain
H2 www.domain
H3. Subdomain
0
Ray PaseurCommented:
I'm wondering where this discussion is going and why (I don't get the h1, h2, h3 part at all).

example.com contains a domain name and a TLD  The domain name is "example" and the TLD is "com"

www.example.com contains a subdomain.  The subdomain is "www" and the domain and TLD are the same as the previous example.

www.example.com/path contains a subdomain, a domain, a TLD and a subdirectory.  The subdirectory is "path"

Armed with those definitions, what is the question now?
0
Ray PaseurCommented:
To anyone coming upon this question in the future, the accepted solution misses the point of the question, and the question itself misses the point of the difference between domains and directories.  I would not rely on this advice.  The author has posted a similar question and maybe we will be able to get a better answer here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28269402.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.