PHP Syntax Question

Hi,

What I am trying to do is this:

if server name = www.mydomain.com or www.mydomain.com/test
Display this

How can I do that?
if ($_SERVER['SERVER_NAME'] ==  "www.mydomain.com"){

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.

hieloCommented:
given this:
 www.mydomain.com/test
$_SERVER['SERVER_NAME'] will report only 'www.mydomain.com'

the "/test" can be obtained from $_SERVER['REQUEST_URI'].

As for the 'x' OR 'y', the "OR" is expressed using "||" (without the quotes):


if( 'www.mydomain.com'==$_SERVER['SERVER_NAME'] || '/test'==$_SERVER['REQUEST_URI']) )
{
...
}
0
Ray PaseurCommented:
Maybe like this...

$url = $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
if ( ($url == 'www.mydomain.com/test') || ($url == 'www.mydomain.com') )
{
    /* DISPLAY SOMETHING HERE */
}

You can find some of these PHP variables by running the script in the code snippet.
<?php phpinfo();

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
hieloCommented:
NOTE: due to "if server name = www.mydomain.com or www.mydomain.com/test
Display this"
if the url is actually http://www.mydomain.com/test

the "Display this" part will always execute because you are already allowing it to do so based on "www.mydomain.com" alone. In other words, the "OR" part will be ignored because "www.mydomain.com" is evaluated first and since it evaluates to true, the "Display this" part will execute.  In other words (based on the way you described/phrased your problem), this would suffice:
if( 'www.mydomain.com'==$_SERVER['SERVER_NAME'] )
{
  echo 'Display this';
}

If you actually want to "Display this" only if the url is "www.mydomain.com/test" (or a variant of it - www.MyDomain.com/test, www.mydomain.com/test/), then what you need is:


if( preg_match('#www.mydomain.com/test/?$#i',($_SERVER['SERVER_NAME'].['REQUEST_URI']) ) )
{
  echo 'Display this';
}
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.