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 ?
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 ?
there is no such variable,
I think you should cut off the "www." in the code.
I think you should cut off the "www." in the code.
Just simply use
$domain = str_replace("www.","",$_SE RVER['HTTP _HOST']);
$domain = str_replace("www.","",$_SE
Problem with str_replace is if your domain is something like 'mysiteonwww.com', you'll come away with just 'mysiteoncom'
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']);
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("/^(.*\.)?([^
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]);
preg_match("/[^.]*\.[^.]*$
echo $domain[0];
$pieces = explode(".", strrev($_SERVER['HTTP_HOST
echo strrev($pieces[0] . "." . $pieces[1]);
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
have you tried simply using
$theString = parse_url($_SERVER['HTTP_HOST']);
$theDomain = $theString['host'];
echo $theDomain;
<?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;
?>
something like this?
$domain = preg_replace("/^www\./", "", $_SERVER['HTTP_HOST']);