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:
Thank you
Stephanie
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.
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'); });
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;
}
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']
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With