create subdomain programmatically in PHP - php

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.

Related

How to automatically assign sudomain to users during registration

I'm currently creating a custom application which involves letting each users have its own subdomain but I'm finding it hard to make that work.
I want user to do this on the fly during registration.
How do I make this work using PHP, .htaccess and MySQL?
This is what I've done so far.
<?php
$host = $_SERVER ['HTTP_HOST'] ;
$subdomain = "myshop" ;
$domain = "$subdomain.$host" ;
echo $domain ;
?>
First point all sub domains to your server with a wildcard DNS-entry
*.example.com
when a user navigates to user1.example.com, have your php script look up the sub domain
$user = array_shift((explode(".",$_SERVER['HTTP_HOST'])));
check if the user exists in the database, or show an error.
You now have the username available and can use that parameter for querying the database.
When a user registers on your site, just create the user in the database and then redirect them to newuser.example.com

CNAME Domain Forwarding Support

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"';
?>

Custom Domain Mapping in CakePHP

I am coding on a social site in CakePHP say example.com, here in the users profiles are located at example.com/profiles/user1. Now I need to provide the functionality to the users to use their custom domains for profiles. For example user1.com should render content from example.com/profiles/user1
Please suggest me the best solution for my problem. I am working on Shared Hosting - Linux Server.
For this functionality, you need to point user1.com to example.com/profiles/user1
For this I can think of two methods:
1. Name server Method:
you can change name servers of user1.com to your example.com name servers like:
ns1.example.com
ns2.example.com
NOTE: This may take 3-4 hours to work
2. URL Forwarding Method:
You can also use URL forwarding to point user1.com to example.com .
This will give you basic idea of forwarding http://support.godaddy.com/help/article/422/forwarding-or-masking-your-domain-name
Assuming that you both have a wildcard record in DNS and that the webserver is configured to serve your Cake project from those hostnames (how to do these things will depend upon your hosting provider), then (with thanks to Subdomaining with Cake), you simply need to place the following in app/config/bootstrap.php:
$subdomain = substr( env('HTTP_HOST'), 0, strpos(env('HTTP_HOST'), '.') );
if( strlen($subdomain)>0 && $subdomain != 'www' ) {
$_GET['url'] = 'user/' . $subdomain . '/' . (isset($_GET['url']) ? $_GET['url'] : '');
}

How to link my site url with cname?

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];

dynamic sub-domain creation with PHP in a linux based server

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
// ...
}
?>

Categories