5

I would like to know how to create a PHP IF Statement that detects if a user is loading/on a certain URL. In my case it's:

www.design.mywebsite.com

The IF Statement needs to cover the whole subdomain, EG the URL will also have to be detected:

www.design.mywebsite.com/filename.php

Once an IF statement is established, I want to add a PHP variable. So it might help others, let's call it:

$myvariable = "Hey there! You're on my subdomain";

So To Sum It Up: The final code should look something like this...

<?php
  if //Code to detect subdomain// {
    $myvariable = "Hey there! You're on my subdomain";
  }
?>
Adam McArthur
  • 950
  • 1
  • 13
  • 27
  • 1
    This has already been asked: http://stackoverflow.com/questions/5292937/php-function-to-get-the-subdomain-of-a-url – Manuel Apr 16 '12 at 09:49
  • Hi there dragon112. I have already looked at that, and yes I was aware of it - but I'm a bit confused as to how I would turn it into a PHP IF Statement. – Adam McArthur Apr 16 '12 at 09:51
  • Thanks for commenting so quickly ogur :). I know that it will require a parse_url(), but I'm not sure where to go from there. – Adam McArthur Apr 16 '12 at 09:53
  • 1
    Well I guess you have to check if array returned from parse_url() (or exploded parts of that array) contains subdomain name you look for, am I right? – ogur Apr 16 '12 at 09:59
  • I think that would have a fair chance at working. Do you reckon you can show me how it's done though as an answer? Cheers. – Adam McArthur Apr 16 '12 at 10:05

1 Answers1

14

A simple solution is

$host = "baba.stackoverflow.com"; // Your Sub domain

if ($_SERVER['HTTP_HOST'] == $host) {
    echo "Hello baba Sub Domain";
} else {
    echo "not baba domain";
}

You can also use parse_url http://php.net/manual/en/function.parse-url.php if you have the URL

$url = "http://baba.stackoverflow.com/questions/10171866/detect-if-a-user-is-on-a-subdomain-and-then-add-a-variable-using-php";
$info = parse_url($url);
if ($info['host'] == $host) {
    echo "Hello baba Sub Domain";
} else {
    echo "not baba domain";
}

Please replace $url with $_GET ;

Baba
  • 94,024
  • 28
  • 166
  • 217