Link to home
Start Free TrialLog in
Avatar of Computer Guy
Computer Guy

asked on

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

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Try to use this:

$WebHost = "domain.com";
if (in_array($_SERVER['HTTP_HOST'], $WebHost){
...
Avatar of Computer Guy
Computer Guy

ASKER

Hi, Looking to make my code work. That didn't.
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!
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?
ASKER CERTIFIED SOLUTION
Avatar of zappafan2k2
zappafan2k2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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
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?
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)
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?
@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
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.
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
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?
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:
https://www.experts-exchange.com/questions/28269402/PHP-Include-Question.html