get subdomain in php - php

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;

Related

Display different text based on domain extension

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

PHP: how to get subdomain: www2 or www35

URL: http://www2.zippyshare.com/v/hlA8HjA9/file.html
I just create a database and I know how to get the filename after the /v/ but now I need to add another string to my database. It's a server.
They've got subdomain as a server and each file is on different server for example www3.zippyshare or www73.zippyshare.com
Now I've got something like this:
$myLink = $_POST['url'];
$var_url = parse_url($myLink);
$var_parts = explode('.', $var_url['host']);
$var = var_parts[0];
But the link save the "v" to my database..
I don't know how to get the subdomain and then skip the www so I will add only the number to my database.
I am not sure if I write it clear enough but I will be really happy if someone could help me.
I'd use this regular expression:
/^(?:https?:\/\/)?([\da-z-]+)\.[a-z]+\.[a-z\.]{2,6}\/v\/[a-z0-9].+\/([a-z0-9]+\.[a-z]+)$/
Do it in PHP like this:
$url = "http://www2.zippyshare.com/v/hlA8HjA9/file.html";
$pattern = "/^(?:https?:\/\/)www([\d0-9-]+)\.[a-z]+\.[a-z\.]{2,6}\/v\/([a-z0-9\/\.].+)$/";
preg_match($pattern, $url, $matches);
list(, $server, $file) = $matches;
The server will then be in $server, the filename will be in $file.
Proof: https://3v4l.org/hHWRm

How to retrieve script name from full absolute URL using PHP?

I'm able to retrieve the full URL like: http://www-click08-co-uk/wonga.php
and I need to retrieve the script name "wonga" from it.
The url will be changing depending on what page the user is on and I will always need the word or phrase after the / and not including the .php, in the example above I would like to create a variable with the value of this being wonga
This is the code I currently have, where "argos" is, is where the database is searched and responds with the information I need, this is where the vaiable would be used
<?php
//-----------------------------------------------------
// Include files and set Classes
//-----------------------------------------------------
require_once $_SERVER["DOCUMENT_ROOT"] . "/includes/common.php";
$db = new dbConnection();
$directorydata = new directorydata();
$phoneDirectory = new phoneDirectory();
$conn = $db->pdoConnect();
// Load the directorydata row via the row ID - 543 is "best buy"
//$directorydata->get($db, 543);
// Load the directorydata row via the url alias field
$directorydata->get($db, "Argos");
// Phone number isn't formatted coming out the DB
$formattedPhoneNumber = $phoneDirectory->formatPhoneNumber($directorydata->Number1);
?>
Just because you didn't provide any code, I provide you a way to solve this on your own:
$url = "http://www-click08-co-uk/wonga.php";
// SEARCH and replace
// FIRST_FUNCTION => google => php trailing name component of path
// SECOND_FUNCTION => google => php explode a string by string
// THIRD_FUNCTION => google => php pop first element of array
$urlParts = SECOND_FUNCTION( ".", FIRST_FUNCTION( $url ) );
echo THIRD_FUNCTION( $urlParts );
OUTPUT:
wonga
An example use of parse_url could be:
$url = 'http://www-click08-co-uk/wonga.php?page=74';
$route = parse_url($url, PHP_URL_PATH);
$routeTokens = explode('/', $route);
$scriptName = array_pop($routeTokens);
echo $scriptName;
which in this case outputs wonga.php.
Just note that this is a very rare task that you would have to take care of yourself. So instead of parse_url you might look at the bigger picture here and start looking for some good MVC framework.

Url Explode for value

Currently I have a url thats like this,
http://website.com/type/value
I am using
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
this to get the value currently but my page has Facebook like's on it and when it is clicked it adds all these extra variables. http://website.com/type/value?fb_action_ids=1234567&fb_action_types= and that breaks that value that I am trying to get. Is there another way to get the specific value?
Assuming you know that this will always be a valid URL, you can use parse_url.
list(, $value) = explode('/', parse_url($url)['path']);
I'd use a preg_replace
explode('/', preg_replace('/?.*$/', '', $url));
You could also use:
$array = explode('/',$_SERVER['PATH_INFO']);
Or, this:
$array = explode('/',$_SERVER['PHP_SELF']);
With this, you do not need the trim() call or the temp var $url - unless you use it from something else.
The reason for two options is I don't know if /type/value is being passed to an index.php or if value is in fact a php file. Either way, one of the two options will give you what you need.

URL parse function

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

Categories