Remove subdomain from $ _SERVER ['SERVER_NAME']

I need to create a script where it will remove the subdomain from $ _SERVER ['SERVER_NAME'] in order to use it in the domain option of the setcookie function to allow access to the cookie on all possible subdomains.

For example, let's say I have

function strip_out_subdomain($domain) { //do something to remove subdomain return $only_my_domain; } $domain = strip_out_subdomain($_SERVER['SERVER_NAME']); setcookie('mycookie', '123', time()+3600, '/', $domain); 

The main problem here is that I do not know the template for my domain. It could be something like:

  • www.mydomain.com
  • subdomain.mydomain.com
  • subdo.mydo.co
  • subdo.subdo.mydomain.com
  • subdo.subdo.mydo.co.uk
  • and etc.

thanks

Stephanie

+7
source share
4 answers

This is a regular expression style for removing part of an additional domain from the fully qualified domain name.

. *?

Operator

makes the wildcard match uneven so that it matches the first point.

 function strip_out_subdomain($domain) { $only_my_domain = preg_replace("/^(.*?)\.(.*)$/","$2",$domain); return $only_my_domain; } 
+4
source

Is it possible to define a variable in the server configuration (httpd.conf, .htaccess)? This requires a bit of additional initial administration, but at least can be done in a central place.

I managed to set a variable in Apache

 SetEnv MY_DOMAIN mydomain.com 

which could be used in PHP:

 $_SERVER['MY_DOMAIN'] 
+3
source

You can request the Alexa service using cURL and extract the hostname without a subdomain:

 function hostname($domain) { $querystring = 'http://xml.alexa.com/data?cli=10&dat=nsa&ver=quirk-searchstatus&uid=19700101000000&userip=127.0.0.1&url='.urlencode($domain); $ch = curl_init(); $user_agent = $_SERVER['HTTP_USER_AGENT']; curl_setopt ($ch, CURLOPT_URL, $querystring); curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt ($ch, CURLOPT_HEADER, 1); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_TIMEOUT, 120); $data= curl_exec($ch); curl_close($ch); preg_match('/\<POPULARITY URL="(.*?)" TEXT="(.*?)" SOURCE="(.*?)"\/\>/Ui',$data,$extract); $hostname = str_replace('/', '', $extract[1]); return($hostname); } 
+2
source

I know this after years, but why not go like this:

 $dom='abcdco.jp'; $sub=preg_replace("/.*?([^\.]+)(\.((co\.\w+)|\w+))$/i",'\1\2',$dom); //strip subdomains 

it prints d.co.jp

where .*?([^\.]+)(\.((co\.\w+)|\w+))$ will mean:

.*? lazy (so that it does not capture the main domain) matches all characters until the next

([^\.]+) correspond to a group of characters that do not contain a period (that is, the main domain or the next domain) ( + , making sure that there is at least one class character) and return it later to \ 1

(\.((co\.\w+)|\w+)) matches the TLD with the previous point, whether it is .co.something or .something and returns it via \ 2 ; the plus sign does the same here

$ binds everything to the end of the line so that we can go all the way from the TLD on the left to the parts of the subdomain, no matter how many of them are

PS I do not know if there are other two-part TLDs, but they can also be added. A quick jump through https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains tells me not, but if there are any, I think they are not so many.

+1
source

All Articles