Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove subdomain from $_SERVER['SERVER_NAME']

I need to make a script where it would remove the subdomain from $_SERVER['SERVER_NAME'] to use it on the domain option of the setcookie function to allow the access of 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 I don't know the pattern 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
  • etc.

Thank you

Stephanie

like image 705
Stef Avatar asked Mar 16 '11 16:03

Stef


People also ask

How do I let PHP create subdomain automatically for each user?

We use the following code: $url=$_SERVER["REQUEST_URI"]; $account=str_replace(". yourdomain.com","",$url); This code just sets the $account variable the same as the subdomain.

How can I get subdomain name in laravel?

It can be used like this: Route::group(array('domain' => '{subdomain}. project. dev'), function() { Route::get('foo', function($subdomain) { // Here I can access $subdomain }); $subdomain = Route::input('subdomain'); });


3 Answers

This is a regex style of removing sub domain part from the full domain name.

The

.*?

operator makes the wildcard matching to be ungreedy so that it matches the first dot.

function strip_out_subdomain($domain)
{
    $only_my_domain = preg_replace("/^(.*?)\.(.*)$/","$2",$domain);
    return $only_my_domain;
}
like image 183
Lenin Raj Rajasekaran Avatar answered Nov 01 '22 21:11

Lenin Raj Rajasekaran


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

I was able to set a variable in Apache

SetEnv MY_DOMAIN mydomain.com

which could be consumed in PHP:

$_SERVER['MY_DOMAIN']
like image 3
KDrewiske Avatar answered Nov 01 '22 22:11

KDrewiske


You can query the Alexa service with cURL and extract the hostname without 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);
}
like image 2
awawjerh Avatar answered Nov 01 '22 21:11

awawjerh