I am working on an php website,
my website url is something like
http://www.xyz.com/hosted/abc.com
I want to access the pages with above url with a cname like http://abc.xyz.com
Means if i type http://abc.xyz.com as url it should internally serve me the pages for http://www.xyz.com/hosted/abc.com.
Please suggest,if anyone know how to achieve this ?Thanks in advance.
If you use bind for dns server you must make a DNS zone manually and add an A record *.xyz.com. into this zone to redirect all the hosts like *.xyz.com to your web server.
Or if you use cPanel go to Simple DNS Zone Editor add an A Record with this name : *.xyz.com. and set Address your server's IP address.
If you use another Control Panel or DNS Server Service you have to read manual to handle requests for *.xyz.com
Then in your php code you can get the requested cname :
$host = $_SERVER['HTTP_HOST'];
$match = preg_match("([a-zA-Z0-9\-])\.xyz\.com");
$cname = $match[0];
Related
Lets say for example my website address is: www.example.com
I let my users each have a website at user1.example.com, user2.example.com - I have achieved this through wildcard sub domains etc.
How can I let my users have these sites (which I host) at specialpage.theirsite.com? They would add a cname record for specialpage pointing to userpages.example.com - How would I handle hosting these pages once they have been pointed? Preferably in PHP!
The best solution is to use CloudFlare wildcard DNS entries to point all unset subdomains to the A record for userpages.example.com and then use a php script in the directory for userpages.example.com to check if the subdomain has been setup.
<?php
$subdomain = array_shift((explode(".",$_SERVER['HTTP_HOST'])));
if ($subdomain IN DATABASE) echo DATABASEOUTPUT;
else echo 'Domain: "' . $subdomain . '.example.com"';
?>
i want to save the whole URL in my database
i get the domain name using
=$_SERVER["SERVER_NAME"]
but how can i get it along with http://www
you write it manually
"http://www.".$_SERVER["SERVER_NAME"];
What about this? :
$self = "http://".$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"]."";
You can even get the arguments after the page name
http://www.webcheatsheet.com/PHP/get_current_page_url.php
Don't forget running on possible non standard ports. And always test to see if your request was done through https, don't test for port numbers to determine ssl.
$uri = "http".($_SERVER['HTTPS']?'s':'')."://".$_SERVER['SERVER_NAME'].(($_SERVER['HTTPS']&&$_SERVER['SERVER_PORT']!='443')||(!$_SERVER['HTTPS']&&$_SERVER['SERVER_PORT']!='80')?":".$_SERVER['SERVER_PORT']:"").$_SERVER['REQUEST_URI'];
$_SERVER['SERVER_NAME'] already contains the "www", if that was the requested host. All you need to append is the protocol (http:// or https://).
You can determine which the site was accessed through by looking at the port -- $_SERVER['SERVER_PORT'] will be 443 for https://
I am on shared hosting and on a add on domain.
I need to create subdomain for each user of my website like if the username is jeff then he should have a url jeff.mydomain.com.
How can I create it programmatically using PHP?
There's two parts to this. Firstly you'll need to setup a wildcard dns entry.
Once you've got that setup you should have all your requests pointed back to a single domain. From there you can then use php to figure out which domain you're currently on:
$domain = $_SERVER['HTTP_HOST'];
$base = 'mydomain.com';
$user = substr($domain, 0, -(strlen($base)+1));// the user part of the domain
if(!empty($user)) {
$user = sanatiseUser($user);
require_once $user.'.php';
}
You need to set apache to listen for all domains coming into a specific IP.
You then need to setup a wildcard DNS entry to point *.domain.com to that IP.
Then inside your app, use $_SERVER['HTTP_HOST'] to determine which user to load.
How can I get real host name by not using $_SERVER['SERVER_NAME'] in PHP? Is there other more reliable way to get it ?
I have created a function which gets host name from the path to the domain.
I would like to avoid using $_SERVER['SERVER_NAME'] variable, because it can be faked by sending modified headers in the HTTP request.
This is my current implementation (this works if the path has an actual domain name in it. For instance: /vhosts/website.com/public_html):
function getServerName() {
$path = realpath(__FILE__);
$url = array();
preg_match_all("/\/[a-z0-9-]+(\.[a-z0-9-]+)+/i", $path, $url);
// 4 is minimum requirement for the address (e.g: http://www.in.tv)
if (strlen($url[0][0]) > 4) {
$result = str_replace("/", "", $url[0][0]);
return $result;
}
else
return false;
}
Thanks!
If you want a server name that can't be set by the client, use $_SERVER['SERVER_NAME']. It is set by the server itself but can also be forged under certain circumstances using a bug, as Gumbo points out and links to in the comments.
I think the one you are referring to is
$_SERVER['HTTP_HOST'];
which, given the HTTP prefix means it comes from the HTTP Headers.
You might want to use:
$_SERVER['SERVER_NAME']
which is defined by the server and can't be changed via a request?
this will get the hostname server-side, but if you're running on a commercial host (not hosting yourself), I don't imagine this will be all that useful.
$host = php_uname( 'n' );
If you're using Apache, what you should do is make your server / site only answer to certain names (else there should be a default that doesn't do much). You can do with with the ServerName and ServerAlias directives.
Edit: as pointed by Gumbo, the original poster probably means HTTP_HOST rather than HOST_NAME. Otherwise, my answer is plain wrong.
The HTTP_HOST variable reflects the domain name that the visitor used to access the site. If doesn't have anything to do with file paths! Its value is conveniently stored in $_SERVER['HTTP_HOST']. Is there any other way to get it? Of course, there're normally several ways to do things. For instance, this works when PHP runs as Apache module.
<?php
$request_headers = apache_request_headers();
echo $request_headers['Host'];
?>
The question is: why would anyone want to do such a thing? Why replace a reliable standard method with a quirky workaround that eventually fetches the same piece of data from the same place?
You have the concern that $_SERVER['HTTP_HOST'] is altered by the HTTP request. Of course it is: that's where it comes from. The browser has to specify what site it wants to visit (that's the base of name based virtual hosts) and if it sends a rogue value, well, it just won't reach the site.
Of course $_SERVER['HTTP_HOST'] can be modified by the client - because in fact IT IS sent by the client. This is part of the http protocol. If you want to get the primary server name defined in the vhost configuration of apache or whatever you can access $_SERVER['SERVER_NAME'] as proposed by the others.
I suggest it is not wise to extract the domain name from the file path of the server (which is stored in __FILE__) as it may render your application non-relocatable (it will no longer be storage location agnostic).
You may see the contents of the array by dumping it within the script using var_dump($_SERVER) but keep in mind the not all web servers and all web server settings expose the same environment. This is documented in the web server documentation and I think it is partly documented in the php online docs.
Update / Important notice: As others pointed out, the content of $_SERVER['SERVER_NAME'] could be spoofed if apache is configured for UseCanonicalName off (which may be a default setting if you are using eg Plesk-based hosting). So actually going with the __FILE__ can solve this (if your doc root contains the host name). The bigger problem of the first approach is that it can be used to inject any sort of stuff into your application (SQL, JavaScript) because php programmers usually take it granted that SERVER_NAME is no user input and thus apply no sanitizing to it.
You don't. That's the purpose of the $_SERVER variables. If you want to get the HOST_NAME from the path, you must first get the PATH from $_SERVER['HTTP_HOST']
I want to create sub-domains using PHP on the fly. Suppose a user registers himself as a name "ABC". Then I want to create a sub-domain named 'ABC.mydomain.com' automatically by PHP. I'm using a linux based server.
Would anyone point me to the right direction?
You should be aware that this is easily done using wildcard DNS records. This way:
you do not have to register each user to your DNS server.
your DNS A-record may contain as few as 1 record: e.g *.mydomain.com -> 12.34.56.78
your web server at 12.34.56.78 have to be configured to accept wildcard
In your server-side scripts, you dynamically resolve "abc.mydomain.com" on your controller/routing code by checking if abc is an existing active username, sample code below:
<?php
// Note that I am using SERVER_NAME vs HTTP_HOST,
// but for additional safety also check your httpd.conf
list($user, $domain) = split("\.", $_SERVER['SERVER_NAME'], 2);
// check if domain is correct,
// or you can leave this part if the web server checks this already
if ($domain === "mydomain.com") {
// here, you verify $user if existent/active
// and reroute or render the page depending on request params
// ...
}
?>