Link to home
Start Free TrialLog in
Avatar of prowebinteractive
prowebinteractive

asked on

get domain name in php

Im want to verify the domain name where my scripts are running

what I started with is

$_SERVER['HTTP_HOST']

the problem with this is that the result could be www.domain.com or domain.com

where I would like it to be domain.com always at all times only because I will be comparing the domain name with a field in the database to verify if they are allowed to be using my application anyone knows if there is a predefined variable for this or do i have to cut off the www.  in code ?

Avatar of snoyes_jw
snoyes_jw
Flag of United States of America image

Nope, I think you have to cut it off in the code.
$domain = preg_replace("/^www\./", "", $_SERVER['HTTP_HOST']);
Avatar of syrma
syrma

there is no such variable,
I think you should cut off the "www." in the code.
Just simply use
$domain = str_replace("www.","",$_SERVER['HTTP_HOST']);
Problem with str_replace is if your domain is something like 'mysiteonwww.com', you'll come away with just 'mysiteoncom'
Avatar of prowebinteractive

ASKER

and what if its a subdomain, or there is no www.
if there's no www, no problem.

The below will return 'domain.com' for all of the following:
www.domain.com
domain.com
subdomain.domain.com
www.subdomain.domain.com

$domain = preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $_SERVER['HTTP_HOST']);
Here are a few other ways to do it:

preg_match("/[^.]*\.[^.]*$/", $_SERVER['HTTP_HOST'], $domain);
echo $domain[0];

$pieces = explode(".", strrev($_SERVER['HTTP_HOST']));
echo strrev($pieces[0] . "." . $pieces[1]);
holy sweet jesus  nice but confusing, it works and thats what counts, I have tried it on every type of domain yet though  such as domain.qc.ca   dpo you know if it will work on this domain name ?


Just a quick pointer - I achieve the same using the IP Address of the server:

   $_SERVER['SERVER_ADDR']

  ..and I compare this with the IP address to which the domain name should resolve.

Basically, this avoids you having to do *any* additional comparison or string massaging.
No, that would just return qc.ca
So, you only want to cut off "www." if "www." is at the beginning?
That's an easy one:

<?php

$_domains = array(
    "www.domain.qc.ca",
    "www.domain.com",
    "www.subdomain.domain.com",
    "domain.qc.ca",
    "www.domain.org",
    "domain.org",
    "subdomain.company.com",
    "mysiteonwww.com",
    "www.mysiteonwww.com",
    "www.www.com"
);

foreach ($_domains as $_domain) {
    echo preg_replace("/^www\./", "", $_domain);
    echo "<BR>\n";
}

?>

--brian
ASKER CERTIFIED SOLUTION
Avatar of tolgaong
tolgaong
Flag of Türkiye image

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
have you tried simply using


$theString = parse_url($_SERVER['HTTP_HOST']);
$theDomain = $theString['host'];
echo $theDomain;

Open in new window

<?php
$string = "kancrutwww.badrun.co.id";

$pos = strpos($string,"www");

$string = substr($string,($pos+4)); //$pos is the location for www, we have to add 4steps to get rid of the "www."

echo $pos."<br>";


echo $string;
?>

Open in new window


something like this?