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 ?

prowebinteractiveAsked:
Who is Participating?
 
tolgaongCommented:
$subject="http://www.experts-exchange.com";
if (preg_match('/\\b(https?|ftp):\/\/www\.(?P<domain>[-A-Z0-9.]+)\\z/i', $subject, $regs)) {
      $result = $regs['domain'];
      }
echo($result);
0
 
snoyes_jwCommented:
Nope, I think you have to cut it off in the code.
$domain = preg_replace("/^www\./", "", $_SERVER['HTTP_HOST']);
0
 
syrmaCommented:
there is no such variable,
I think you should cut off the "www." in the code.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
gileze33Commented:
Just simply use
$domain = str_replace("www.","",$_SERVER['HTTP_HOST']);
0
 
snoyes_jwCommented:
Problem with str_replace is if your domain is something like 'mysiteonwww.com', you'll come away with just 'mysiteoncom'
0
 
prowebinteractiveAuthor Commented:
and what if its a subdomain, or there is no www.
0
 
snoyes_jwCommented:
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']);
0
 
snoyes_jwCommented:
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]);
0
 
prowebinteractiveAuthor Commented:
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 ?
0
 
siliconbritCommented:


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.
0
 
snoyes_jwCommented:
No, that would just return qc.ca
0
 
Brian BushSolutions ArchitectCommented:
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
0
 
zanus123Commented:
have you tried simply using


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

Open in new window

0
 
zparksCommented:
<?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?
0
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.

All Courses

From novice to tech pro — start learning today.