homepage Welcome to WebmasterWorld Guest from 54.204.141.129
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Dynamic Sub-Domain with PHP
What's solution!?
webvand




msg:1308395
 9:10 am on Jun 19, 2005 (gmt 0)

I want to create subdomain(s) dynamically for user registration etc. Any idea please?

 

Blackie




msg:1308396
 12:14 pm on Jun 19, 2005 (gmt 0)

To give you a start, here is an example which I use on one of my sites:

$site_url = strtolower($_SERVER['HTTP_HOST']);
if(strpos($site_url,':')) $site_url = substr($site_url,0,strpos($site_url,':'));

if ($site_url == 'example.org') header('Location: http://www.example.org');
elseif ($site_url!= 'www.example.org')
{
if(strpos($site_url,'example.org')) $host_length = strlen('example.org');

$user = substr($site_url,0,strlen($site_url)-$host_length-1);

header("Location: http://www.example.org/userpage_redirect.php?user=$user");
}

webvand




msg:1308397
 1:04 pm on Jun 19, 2005 (gmt 0)

Thanks for replay blackie.
I guess you got it wrong! Anyway, in continue of explanation:
I need a solution for a "Weblog system" etc. so, when each new user tries to register, a directory on the server will create for her/him. (www.example.com/user_name) but as you know I need her/him directory to be available as follows too (http://user_name.example.com/).
It's not a local project, so the _conf file of Apache Server may not accessible at all. I need a working .htaccess code etc. or something else.

anshul




msg:1308398
 6:19 am on Jun 20, 2005 (gmt 0)

I also thought work-about for that. But I suppose, a directory won't point to a subdomain without writing VirtualHost in httpd.conf. I can't hope if this is possible using .htaccess ( to create a new subdomain VirtualHost ).

Who are doing this ( eg, rediff blogs ) is perhaps using some other technique. They do this without restarting Apache? Then they are not writing into httpd.conf?

sumguyovrthar




msg:1308399
 4:04 pm on Jul 14, 2005 (gmt 0)

I needed the same question answered, and found that if you create a directory in the directory htdocs it makes a subdomain named that directory on some servers. is there some way to create directories with php?

Anyango




msg:1308400
 5:36 pm on Jul 14, 2005 (gmt 0)

Hey!

I do not know about doing it like DNS wise but i have been doing this alot with Dynamic php code + use of .htaccess files.

I mean you 'll only have your htdocs as the directory and my code wont create any sub directory for any sub domain but it can catch your sub-domain request and will return you a page you made for that sub-domain. that page can either be stored in db or can be actual physical page on your server.

If this is irrelevant, I am sorry. but if it can help you , please Reply, i ll post my code.

Thank You

Kami

sumguyovrthar




msg:1308401
 6:00 pm on Jul 14, 2005 (gmt 0)

I would be very interested in seeing your code, Kami. i think it would be better to be redirected to the page from the subdomain then to create a new page/directory every time a new user registers.

Anyango




msg:1308402
 6:30 pm on Jul 14, 2005 (gmt 0)

Thanks, Here it is

But first you need to set your main domain to act as a catch-all subdomain. its like putting ServerAlias * so that each subdomain which is requested
to the main domain reaches the same place. I mean if your main domain root is

/home/admin/abc.com/htdocs/

then your catch-all setup shall send all sub-domain requests to that same directory

/home/admin/abc.com/htdocs/

and then in the index.php file of this directory you can do this

<?
global $domainname;
global $subdomainname;
$domainarray = explode('.', $_SERVER['HTTP_HOST']);
$index=count($domainarray)-1;
$domainname= $domainarray[$index-1].".".$domainarray[$index];
$subdomainname="";
for($i=0;$i<$index-1;$i++)
{
if($subdomainname=="")
{
$subdomainname=$domainarray[$i];
}
else
{
$subdomainname=$subdomainname.".".$domainarray[$i];
}

}

ShowCustomizedPageForSubDomain($subdomainname);

?>

This function ShowCustomizedPageForSubDomain($subdomainname) can be easily implemented in two ways:

1) Either your store Page's Html in your database and all this function would do is to pick up that html code for the provided sub-domain from the database and simply
"echo" or "print" it.
2) or you can simply have standard actual pages for each subdomain and you simply include them here like this

ob_start();
include "http://url_of_the_page_you_want_to_show";
$data=ob_get_contents();
ob_clean();

this Object buffer will return you that page's content in the variable "$data"; you simply echo $data;

hmm , going more deep, Second Implementation of your function is like this

function ShowCustomizedPageForSubDomain($subdomainname)
{
ob_start();
include "http://url_of_the_page_you_want_to_show_for_this_subdomain";
$data=ob_get_contents();
ob_clean();
echo $data;
}

Please feel free to discuss anything or ask anything if you like to.

Thanks
Kami

sumguyovrthar




msg:1308403
 6:59 pm on Jul 14, 2005 (gmt 0)

thanks, that helps me alot. i think this could be useful to webvand also.

Anyango




msg:1308404
 8:00 pm on Jul 14, 2005 (gmt 0)

You are most Welcome. I am glad it helped you.

Anyango




msg:1308405
 11:40 pm on Jul 15, 2005 (gmt 0)

webvand

it didnt do any help?

webvand




msg:1308406
 5:23 am on Jul 16, 2005 (gmt 0)

Wow!
Dear Kami, your method is interesting and it made me glad because I guess it would help me in my way. I'm going to check it out but could you please tell me how to set my domain to act as a catch-all subdomain? (It's running on Apache Server with CPanel Control Panel). Also is there any '.htacess' code required additionally?!
Thanks a bunch and I like to contact with you more.

PS. sumguyovrthar, Thanks for reply.

Anyango




msg:1308407
 1:52 pm on Jul 16, 2005 (gmt 0)

Hey Bro!

Thanks for your appreciation, i am glad i can help you.

1) To setup a domain to have a catch-all style will be to open your apache's httpd.conf file and for the domain which you want to set to be catch-all you can put this

ServerAlias *.your-domain-name.com

if you donot have access to your httpd.conf directly then you might be able to edit apache parameters for your domain in your cpanel and even if that is not possible then you can simply send an email to your support team to enable catch-all on your domain.

2) .htaccess code will be needed as you ll proceede with the creation of pages, mostly it would be needed to make your dynamic pages look static to your visitors and to bots. for example

you might need to have a page

user1.yourdomain.com/user-profile.html

instead of saying user1.yourdomain.com/user-profile.php?user=user1

RewriteEngine On
RewriteRule ^([^/]+)/?\.htm$ showpage.php?page=$1 [L]
RewriteRule ^([^/]+)/?\.html$ showpage.php?page=$1 [L]

The above code will capture all .html and .htm file requests to your domain or subdomain and it will send the pagename which was requested to your file showpage.php

you can retrieve pagename in your showpage.php like

$page=$_REQUEST['page'];

then you can call your functions as i mentioned earlier to show this page.

Once your catch-all setup is done we will proceede with setting up these things. and please be assured that even if by any means you do not get any access to apache's configuration, even then we can find a way to sort this out.

When there is a Will, There is a way.

Cheers

webvand




msg:1308408
 8:18 am on Jul 17, 2005 (gmt 0)

Hmm...

Kami, as you may know I have no direct access to my Apache '.conf' file so I'm going to contact my website's support team right now. However it's nice of you to explain these steps clearly. Thanks a bunch.

Kindly regards, Behnam

webvand




msg:1308409
 8:42 am on Jul 18, 2005 (gmt 0)

Dear Kami,
My domain acts as a catch-all subdomain right now but there is something wrong yet! I’ll try to explain every thing and please tell me what is the problem?

”http://[anything].mydomain.com” will redirect to [mydomain.com...] (Note: not to www.mydomain.com) so, I’ve used your code as the following:

===================================================
<?
global $domainname;
global $subdomainname;
$domainarray = explode('.', $_SERVER['HTTP_HOST']);
$index=count($domainarray)-1;
$domainname= $domainarray[$index-1].".".$domainarray[$index];
$subdomainname="";
for($i=0;$i<$index-1;$i++)
{
if($subdomainname=="")
{
$subdomainname=$domainarray[$i];
}
else
{
$subdomainname=$subdomainname.".".$domainarray[$i];
}

}

ShowCustomizedPageForSubDomain($subdomainname);

function ShowCustomizedPageForSubDomain($subdomainname)
{
ob_start();
include "http://www.mydomain.com/behnam/index.php";
$data=ob_get_contents();
ob_clean();
echo $data;
}

?>
===================================================

1st. I should to say, as you know I missed my current REAL-SubDomains as all of them are redirected to the root (and without ‘www’!) So it’s not good for me at this time. Any way, I just want to handle what’s the referral path? $subdomain contains nothing. I’ll trace first part of the script below.
$domainarray: [0] “mydomain” &[1] “com”
$index: NULL! (It contains nothing)
$domainname: “mydomain.com” string exactly.
And so on. Is it enough for getting what’s wrong?

PS. One thing more: all of the sub-domain inputs are redirected to included path but why $subdomain is same? (Just an empty var) So, there’s no difference between entered subdomains now!

Thanks, Behnam

Anyango




msg:1308410
 10:20 am on Jul 18, 2005 (gmt 0)

Hey!

No problem bro! we can handle this situation. i ll reply to your message in a few hours

Cheers!

Kami

Anyango




msg:1308411
 7:52 pm on Jul 18, 2005 (gmt 0)

Hey

Actually we have had this same problem with one of our friends when he setup catch-all subdomain system , all his sub domain requests started changing to "www." which was definitely not needed behaviour.

Are you sure your admin guys have setup that correctly in apache? is that an *.domain-name.com Server alias or something else?.

Number 2 . The reason you are seeing $subdomain variable="" is simply because of the dns and server setup all requests are automatically getting redirected to [yourdomain.com...]

so when the code gets this path

[yourdomain.com...]

it will show
$domainname="yourdomain.com"
$subdomainname=""

just because there is no any sub-domain in the user url request.

This is definitely DNS issue or domain setup issue. Please send an email to support guys saying that you want to recieve

anything.yourdomain.com as [anything.yourdomain.com...]

not [yourdomain.com...]

and when this gets resolved and we start getting requests as

[anything.domain.com...]

then

code will show

$domainname="domain.com"
$subdomainname="anything"

Please let me know their response.

I look forward to hearing back.

Thank You

webvand




msg:1308412
 8:25 am on Jul 19, 2005 (gmt 0)

Hey bro,

Exactly what I’ve guessed. I’ve sent the request to admin team and waiting for their reply.
Just don’t know how to appreciate at this time!

Thanks in advance, Behnam

webvand




msg:1308413
 8:10 am on Jul 23, 2005 (gmt 0)

Hey,

I've just received the support team answer as the following:

----------------------------------------------------
What I did was
- First I modified the DNS zone of domain.com to accept all *.domain.com requests with an A record of ***.**.***.** (Your srv IP Address)
- Next, edited Apache's conf to accept *.domain.com
- And lastly edited your /home/domain/public_html/.htaccess to redirect any subdomain(*.domain.com) to [domain.com...]

So now, in your case all you have to do is edit your .htaccess file and tune it to your particular requirements.
----------------------------------------------------
So, I need the content of .htaccess file that I'm totally unfamiliar with.

Thanks in advance, Behnam.

Anyango




msg:1308414
 3:16 pm on Jul 25, 2005 (gmt 0)

Hey Bro

that guy did good things in step 1 and 2 but not in step 3 ;)

"And lastly edited your /home/domain/public_html/.htaccess to redirect any subdomain(*.domain.com) to [domain.com...] "

I think he tried to over-serve ;)

you see according to his statement he is redirecting any subdomain *.domain.com to [domain.com...] using the .htaccess placed in

/home/domain/public_html/.htaccess

that means

that the request for any subdomain is successfully reaching your root folder

/home/domain/public_html/

in other words

if i type

subdomain1.domain.com

it reaches

/home/domain/public_html/

for your domain successfully and after it reaches then it is being redirected,

if it reached, we got what we wanted, no need to redirect, take a backup of htaccess placed here

/home/domain/public_html/.htaccess

and then delete it

we will write our own .htaccess later, after deleting this .htaccess and running the same script as we already discussed you will surely get all the subdomains

and the domain name in respective variables.

Leme know if it has started to work

cya ;)

Anyango




msg:1308415
 3:17 pm on Jul 25, 2005 (gmt 0)

we dont need any .htaccess for our subdomain thing, we will need .htaccess to have seo friendly urls thats it

Mr_Fern




msg:1308416
 9:57 pm on Jul 25, 2005 (gmt 0)

With the DNS Zone file edited to catch * for all IN A ***.***.***.***

And the Apache modified

You'd add this code to very top of your index page right after "<?php".

-------------------------------------

$domain = $_SERVER['HTTP_HOST'];
$domain_parts = explode('.',$domain);
if (count($domain_parts) == 3 && $domain_parts[0]!= "www") { // make sure a subdomain is called

$user = $domain_parts[0];
$loc = "http://domain.tld/whateverpage.ext?varname=$user";
@header("Location:$loc");
}
-------------------------------------

That code should do the trick. You won't need to touch the .htaccess file

webvand




msg:1308417
 6:59 am on Jul 26, 2005 (gmt 0)

Dear Kami,

As I removed .htaccess file, everything works fine now! You’re the man. I really don’t know how to appreciate you. It was a long time that I really needed this ability for my projects in mind but couldn’t find the solution.

Still can't find right word(s) to appreciate.
Kindly Regards, Behnam.

PS. Did you received my message in your sticky mail inbox?
PSS. Mr_Fern Thank you for reply.

sujanaik




msg:1308418
 10:27 am on Aug 10, 2005 (gmt 0)

Dear Kami,

Hi, sujata here,I'm new to programming. I have gone thru the forums of creating sub domains. I need ur help.

My site is : www.example.com, i want to create subdomains for any new user who registers. For new user a userid is generated like (20050709-115700189). I want to show url in this format www.20050709-115700189.example.com. This page should have contents of http://example.com/clients/links4.php?unid=20050709-115700189.
I'm not understanding where to put the code that u have provided, bcoz u have mentioned to put in index.php, but after login only the page gets the userid. can u help me.
waiting for your response.

[edited by: jatar_k at 4:39 pm (utc) on Aug. 10, 2005]
[edit reason] examplified [/edit]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved