7

In PHP, how can I check if the current page has www in the url or not?

E.g if the user is on http://www.example.com/something , how can I find that there's www in the URL, and on http://example.com/something , www isn't in the url?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • Keep in mind if you use any of the below methods check to make sure $_SERVER['HTTP_HOST'] exists before doing anything. if (isset($_SERVER['HTTP_HOST'])) Not every hosting environment will populate that variable. I've run across a few that don't. It would be best to use both HTTP_HOST and SERVER_NAME to ensure you can get a domain to check. – kittycat Nov 12 '12 at 00:27
  • @cryptic is SERVER_NAME always present? – Ali Nov 12 '12 at 00:35
  • 1
    http://stackoverflow.com/questions/2297403/http-host-vs-server-name will give some good info on both. If the SERVER_NAME is not set on Apache a reverselookup will be done on the IP. If your domain has a dedicated IP it should resolve to correct domain, but if it's is a shared IP you run the risk of the domain being returned being a different domain. – kittycat Nov 12 '12 at 00:40

6 Answers6

11

You can use $_SERVER['HTTP_HOST'] to get your domain. Now, if your site does not use sub domains that's easy. Just use something like this:

$url1 = "www.domain.com";
$params = explode('.', $url1);

if(sizeof($params === 3) AND $params[0] == 'www') {
    // www exists   
}

Note that if your params array has 2 items, then domain looks like this domain.com and there is no www clearly. You can even check size of $params array and decide if there is www or not based on its size.

Now if your site uses sub domains then it get's more complicated. You would have these as possible scenarios:

$url1 = "www.domain.com";
$url2 = "www.sub.domain.com";
$url3 = "domain.com";
$url4 = "sub.domain.com";

Then you would again explode every url, and compare size and check if first item is 'www'. But note that your sub domain could be named www where www would be sub domain :D And then it would look like this: www.domain.com, and again, www is sub domain :) That's crazy I almost gone mad on this one :)

Anyway, to check for www when site is not using sub domains, that's peace of cake. But to check if url has www and your site is using sub domains, that could be tough. The simplest solution to deal with that problem would be to disable www sub domain, if you can.

If you need more help and this does not answer your question, feel free to ask. I had similar situation where users would be able to create their own accounts that would be sub domains, like www.user.domain.com, and I had to do the same thing.

Hope this helps!

Matija
  • 2,610
  • 1
  • 18
  • 18
7

you could do a preg_match this would be:

if(preg_match('/www/', $_SERVER['HTTP_HOST']))
{
  echo 'you got www in your adress';
}
else
{
  echo 'you don not have www';
}

you could even do it with .htaccess depending on what you are building the above option would do fine for a simpel detection.

if you pick the option Ferdia gave do the check as following:

$domain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
if(in_array('www', $domain))
{
    echo 'JUP!';
}
John In't Hout
  • 304
  • 1
  • 10
  • 1
    Your regex is wrong, what if he has www somewhere at the end of URL string. Check this: http://rubular.com/r/IEzoGnqEm1 – Matija Nov 12 '12 at 00:49
  • His question was "www in the url" not mentioning it should be at the start. However, if you would only want to match the www at the start, the regex would be: /^www/ – Jos Gerrits Mar 17 '15 at 14:09
4

Do you already know the url? Then take this:

<?php
$parsed = parse_url("http://www.example.com/something");
$hasWww = strpos($parsed['host'], "www.") === 0;
var_dump($hasWww);

Otherwise, take

 strpos($_SERVER['HTTP_HOST'], "www.") === 0
David Müller
  • 5,291
  • 2
  • 29
  • 33
2

There are so many ways to check whether www contains or not.

one of the way is:

if(strpos($_SERVER['HTTP_HOST'], `www`) !== false) {
    echo "WWW found";
} else {
    echo "WWW not found";
}
Saud Khan
  • 786
  • 9
  • 24
1
array_shift(explode(".",$_SERVER['HTTP_HOST']));

Should work, then examine the array for the presence of www. If you are simply trying to remove or force www, then a .htaccess rule is by far superior to PHP for that job.

Ferdia O'Brien
  • 867
  • 3
  • 10
  • 25
  • Does HTTP_HOST include the www part? I thought it only includes the domain.. I could be wrong though – Ali Nov 11 '12 at 23:56
  • The subdomain is considered part of the host, so yes, it should contain the www – Andrew Nov 11 '12 at 23:58
1

From the manual

$_SERVER['HTTP_HOST']

Contents of the Host: header from the current request, if there is one.

$_SERVER['SERVER_NAME']

The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106