Apologize if this has already been asked and answered; did a quick search, but, not exactly sure how to word it/what exactly to search for.
I have to web addresses pointing to one site/file(s). One ends in .net [domain.net] and the other ends in .org [domian.org].
Using PHP; I want to put at least the ".net" and/or ".org" part of the URL into a variable to determine what text is displayed.
Something to the effect of:
$domainExt = 'net';
For domain ending with .net; as an example.
Thanks for any help, tips, etc.
First get the extension
$host = filter_input(INPUT_SERVER, 'HTTP_HOST');
$domainExt = pathinfo($host, PATHINFO_EXTENSION);
Then switch through the extension
switch($domainExt){
case "net":
$var = "yada";
break;
case "org":
$var = "yada yada";
break;
}
You can apply this example to fit your needs
You can use environment information.
$_SERVER['HTTP_HOST']
or
$_SERVER['SERVER_NAME']
You can use below code
<?php
$host = filter_input(INPUT_SERVER, 'HTTP_HOST');
$domainExt = substr(strrchr($host, "."), 1);
?>
Just explode the current HTTP_HOST at the period.
$ext = explode( '.', $_SERVER['HTTP_HOST'] );
if( $ext == 'org' ){
echo 'This is .org';
} else {
echo 'This is .net';
}
Use parse_url() function to get the domain extention.
Example below:
$url = 'http://www.example.com/site';
echo end(explode(".", parse_url($url, PHP_URL_HOST)));
// output "com"
Dynamically get the domain extension like below way.
$url = 'http://' . $_SERVER['SERVER_NAME'];
echo end(explode(".", parse_url($url, PHP_URL_HOST)));
// output "com"
Try to separate the website on different folder for .net and .org and use Virtual Host to route the domain names on their respective folders.
sorry for my English but i hope you get the point.
If you are using apache then google search can help you
Related
Is there any predefined method in PHP to get sub-domain from url if any?
url pattern may be:
http://www.sd.domain.com
http://domain.com
http://sd.domain.com
http://domain.com
where sd stands for sub-doamin.
Now method must return different values for every case:
case 1 -> return sd
case 2 -> return false or empty
case 3 -> return sd
case 4 -> return false or empty
I found some good links
PHP function to get the subdomain of a URL
Get subdomain from url?
but not specifically apply on my cases.
Any help will be most appreciable.
Thanks
Okay, here I create a script :)
$url = $_SERVER['HTTP_HOST'];
$host = explode('.', $url);
if( !empty($host[0]) && $host[0] != 'www' && $host[0] != 'localhost' ){
$domain = $host[0];
}else{
$domain = 'home';
}
So, there are several possibilities...
First, regular expressions of course:
(http://)?(www\.)?([^\.]*?)\.?([^\.]+)\.([^\.]+)
The entry in the third parenthesis will be your subdomain. Of course, if your url would be https:// or www2 (seen it all...) the regex would break. So this is just a first draft to start working with.
My second idea is, just as yours, explodeing the url. I thought of something like this:
function getSubdomain($url) {
$parts = explode('.', str_replace('http://', '', $url));
if(count($parts) >= 3) {
return $parts[count($parts) - 3];
}
return null;
}
My idea behind this function was, that if an url is splitted by . the subdomain will almost always be the third last entry in the resulting array. The protocol has to be stripped first (see case 3). Of course, this certainly can be done more elegant.
I hope I could give you some ideas.
Try this.
[update] We have a constant defined _SITE_ADDRESS such as www.mysite.com you could use a literal for this.
It works well in our system for what seems like that exact purpose.
public static function getSubDomain()
{
if($_SERVER["SERVER_NAME"] == str_ireplace('http://','',_SITE_ADDRESS)) return ''; //base domain
$host = str_ireplace(array("www.", _SITE_ADDRESS), "", strtolower(trim($_SERVER["HTTP_HOST"])));
$sub = preg_replace('/\..*/', '', $host);
if($sub == $host) return ''; //this is likely an ip address
return $sub;
}
There is an external note on that function but no link, So sorry to any original developer who's code this is based on.
I have following urls.
reservation.abchotel.com
booking.abchotels.org
abc1.abc.dev
I want to get sub domain from above urls.
ex:-
.abchotel.com
.abchotels.org
.abc.dev
How I do it? I'm using zend feamwork. Please help me. What is the best solution?
It seems you don't want the subdomain but the domain. Because what you listed are not the subdomains.
The following pieces of knowledge will enable you to successfully deal with domain names.
Zend_Validate_Hostname - also good to look at the code
PHP Server variables
String manipulation in general. Hostnames are easy to in- and explode since they're always delimited by dots.
PHP parse_url
See also[this question on SO on how to get subdomain(s) from an url.
If they are always in that form you could do something like the following (assuming $url is set).
$split_url = explode(".", $url);
$subdomain = ".".$split_url[1].".".$split_url[2];
Or did you want to know how to get the URL in the first place too, or to allow for more than 3-level domains?
A very simple way to get domain and subdomain :
$parts = explode('.', $_SERVER['HTTP_HOST']);
$domain = '.' . implode( '.', array_reverse(
array(
array_pop($parts),
array_pop($parts)
)
);
$subdomain = implode('.',$parts);
$url = $_SERVER["SERVER_NAME"];
$replace_domains = array(
".abchotel.com" => "",
".abchotels.org" => "",
".abc.dev" => "");
$url = str_replace(array_keys($replace_domains), array_values($replace_domains), $url);
echo $url;
This is probably quite hard to explain, so I'll try to make it as simple as possible:
Here's my code:
<?php
$ref = $_SERVER['HTTP_REFERER'];
switch($ref) {
case "http://www.facebook.com":
$ref_name = "Facebook";
break;
case "http://www.twitter.com":
$ref_name = "Twitter";
break;
}
?>
From what I know, HTTP_REFERER pulls the entire referrer url (e.g. www.facebook.com/abc/xyz/mno=prq) as oppose to the top-level domain. I'd like to be able to match $ref against something so that all referrer's whether from say http://static.facebook.com (a sub-domain) or http://www.facebook.com/profile_id/bla (a url with additional folders and parameters after the top-level domain) are given the value of "http://www.facebook.com".
What's the most simple and effective way to do so?
Any comments/answers etc will be greatly appreciated :)!!
See: parse_url
$ref = 'http://static.facebook.com';
$host = implode('.', array_slice(explode('.', parse_url($ref, PHP_URL_HOST)), -2));
switch ($host) {
case 'facebook.com':
break;
case 'twitter.com':
break;
}
Update: Have a look at Root Zone Database if you're dealing with special TLDs.
I am suffering from a problem while i take the current url of the page and spliting them into parts and then checking for the index.php phrase.So far i have done this:
<?php
$domain=$_SERVER['REQUEST_URI'];
$values = parse_url($domains);
$path = explode('/',$values['path']);
if(array_search($path[2], "index.php"))
{
echo "hello";
}
?>
but its not working so guys help me out and thank you in advance coz i know i will be satisfied by your answers.
Try this:
$pathToFile = $_SERVER['PHP_SELF'];
$currentFilename = substr($pathToFile, strrpos($pathToFile, '/') + 1);
if($currentFilename == 'index.php')
{
echo 'This file is index.php!';
}
$_SERVER['PHP_SELF'] is the path to the current file on the local system. Since you don't care about the domain name or the query string, this is easier.
strrpos($pathToFile, '/') gets the index of the last occurrence of / in $pathToFile.
substr($pathToFile, strrpos($pathToFile, '/') + 1) get the portion of $pathToFile starting with the character after the index found by strrpos() in step 2.
You should be left with only the filename in $currentFilename, which you can compare with whatever you choose.
Note that this will match any index.php file, not just the one at your domain root. For example, if your site is located at http://example.com, http://example.com/subdir/index.php would also be true for $currentFilename == 'index.php'. If that's not what you want, you'd do it a little differently.
Use this:
$domain=$_SERVER['REQUEST_URI'];
$path = explode('/',$domain);
if(array_search($path[2], "index.php"))
{
echo "hello";
}
I'm not sure what parse_url() is, but it didn't seem to do anything in your code.
Given this variable:
$variable = foo.com/bar/foo
What function would trim $variable to foo.com ?
Edit: I would like the function to be able to trim anything on a URL that could possibly come after the domain name.
Thanks in advance,
John
Working for OP:
$host = parse_url($url, PHP_URL_HOST);
The version of PHP I have to work with doesn't accept two parameters (Zend Engine 1.3.0). Whatever. Here's the working code for me - you do have to have the full URL including the scheme (http://). If you can safely assume that the scheme is http:// (and not https:// or something else), you could just prepend that to get what you need.
Working for me:
$url = 'http://foo.com/bar/foo';
$parts = parse_url($url);
$host = $parts['host'];
echo "The host is $host\n";
I'm using http://www.google.com/asdf in my example
If you're fine with getting the subdomain as well, you could split by "//" and take the 1th element to effectively remove the protocol and get www.google.com/asdf
You can then split by "/" and get the 0th element.
That seems ugly. Just brainstorming here =)
Try this:
function getDomain($url)
{
if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE)
{
return false;
}
/*** get the url parts ***/
$parts = parse_url($url);
/*** return the host domain ***/
return $parts['scheme'].'://'.$parts['host'];
}
$variable = 'foo.com/bar/foo';
echo getDomain($variable);
You can use php's parse_url function and then access the value of the key "host" to get the hostname