Using PHP to Create a Subdomain

He has: 1,380 posts

Joined: Feb 2002

Basically, I want to have a user-run script, that lets them create a subdomain of my domain with their username. I searched around google, but couldn't come up with any answers.

How do you use PHP to create a subdomain?

Thanks (Please provide examples)!

He has: 1,758 posts

Joined: Jul 2002

The only way to actually CREATE the actual subdomain itself with PHP would be to use PHP to rewrite the httpd.conf file (on apache) to add the sub domains then you'd probably need to restart the webserver to put it into effect.

This isn't something you'd want to muck about with unless your absolutly sure of what you're doing and you'd have to have your own webserver as well.

*Thinks...*

Now if you have a wildcard subdomain on your account (ie everything.yourdomain.com) that always takes the user to the same place you could detect the subdomain using $_ENV[HTTP_HOST] then redirect the user to the appropriate page or pull the relevent content whereever its coming from based on that instead.

Andy

He has: 1,380 posts

Joined: Feb 2002

how about like a cron job? what i'm looking for ais an automated subdomain registration...kind like angelfire or w/e does to give the users their own area...

example: i sign up as "myjob52" at angelfire...they give me an account as "myjob52.angelfire.com

He has: 1,758 posts

Joined: Jul 2002

So your looking to setup a "free" web hosting company?

If you give me more information about the project as a whole It'll make it much easier for me to suggest a plan of action for you.

Andy

He has: 1,380 posts

Joined: Feb 2002

No....

I know how I want everything else to run, and I'd rather not talk directly about what I'm working on, but I can't figure out how do to an automated subdomain creation, MUCH LIKE angelfire or a free hosting company

He has: 1,758 posts

Joined: Jul 2002

The solution would vary depending on the exact nature of what you want to do.

For instance, if you want to provide a personalised startpage service like Yahoo would then you don't need to create an "actual" sub domain, just detect what was entered in the address bar and display the appropriate content.

If you are actually hosting something then you will need to add info to the end of the httpd.conf file (on apache - other webservers vary). This can be done at the time they register, or you could write all the details to a tempory location (ie a database, text file, etc) then apend the whole lot together at a specified time (for instance via a cronjob).

Of course... you can only do this if your running the stuff on your own personal web server and not a shared hosting platform.

I really can't be any more specific unless you can be more specific about your setup etc...

Andy

He has: 1,380 posts

Joined: Feb 2002

Well, I'm going to have my own virtual server...but not dedicated. I know it's Linux based, but past that I don't know.

Its more along the lines of the hosted content is what I'm looking for.

He has: 1,758 posts

Joined: Jul 2002

Then the first way I suggested should work, as long as your hosting provider can set you up a wildcard subdomain. to do that they need to setup a wildcard a record in your dns *.yourdomain.com and then setup the hosting account the sameway. That's something you may have to ask nicely for.

Once they've done that for you, let me know and I'll give you some example code.

Andy

He has: 1,758 posts

Joined: Jul 2002

I couldnt wait... something like this should get you started:

<?php

$host
= $_ENV['HTTP_HOST'];

$subdomain = explode(".",$host);
$subdomain = $subdomain[0];

$sql = "SELECT * FROM `database` WHERE username = '$subdomain' LIMIT 0,1";

//etc, etc, etc...

?>
'

of course... you'll have to do some checking to ensure that:

a) they've entered the subdomain.
b) the subdomain isn't www.
c) the subdomain is a valid user

in all of these circumstances you'll have to redirect to your default homepage, but a simple if statement will sort that one for you.

Andy

He has: 1,380 posts

Joined: Feb 2002

ok thanks

will that be more of a pain than actually editing the httpd.conf file?

He has: 1,758 posts

Joined: Jul 2002

no... and since you don't have your own webserver I very much doubt your hosting provider will let you muck about with the httpd.conf anyway. (a virtual server is just a chunk of shared space on an existing server).

Andy

He has: 1,380 posts

Joined: Feb 2002

yea i knew it was shared...i was just saying

alright, i'll give it a try. thanks.

They have: 2 posts

Joined: Sep 2004

Wouldn't using mod_rewrite in .htaccess also work just as well, though?

openmind's picture

He has: 945 posts

Joined: Aug 2001

TBH the solution that Andy has come with is the best one. I too have used a simple script in CF that works in exactly the same way and works great with a * sub-domain.

It's the easiest and simplest way to do it without mucking with IIS in my case...

heebiejeebieclu's picture

They have: 527 posts

Joined: Aug 2004

go to http://www.hotscripts.com and type in myLounge. This is a thing to create your own subdomains

They have: 2 posts

Joined: Sep 2004

okay thanks

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.