How to create subdomain dynamically by PHP ?

Discussion in 'PHP' started by mohamed79, Apr 5, 2009.

  1. #1
    Hello

    i want to know how to create subdomain dynamically by PHP ?

    Thanks for good co-op
     
    mohamed79, Apr 5, 2009 IP
  2. paul_so40

    paul_so40 Peon

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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); 
    
    ?> 
    PHP:
    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(); 
    PHP:
    this Object buffer will return you that page's content in the variable "$data"; you simply
    echo $data; 
    PHP:
    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; 
    } 
    PHP:
     
    paul_so40, Apr 5, 2009 IP
  3. mohamed79

    mohamed79 Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i host my website in GoDaddy , this code didn't worked there
     
    mohamed79, Apr 5, 2009 IP
  4. young coder

    young coder Peon

    Messages:
    302
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i think you have to won a server , at least VPS to do that
    because you are not allowed to do this thing on shared hosting

    really if you don't have a server , then just use a folder :)
     
    young coder, Apr 5, 2009 IP
  5. Glen2007

    Glen2007 Banned

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I faced the same problem some time ago and VPs wont able to do that. I'm not sure if VPS had changed but at that time I was required to have full dedicated server with root access.
    Ask your provide to run the script for you.
    Thanks
     
    Glen2007, Apr 5, 2009 IP
  6. fourfingers

    fourfingers Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What you're looking for is called wild-card subdomains and you can get the low down on getting it set up here ( hxxp://www.google.com/search?q=wildcard+subdomains+tutorial ) ... several good tutorials there.

    I've heard that hostgator will set them up on some of the shared boxes but you have to get the right tech support guy. If you try and he says no, wait a week and try again.

    Any vps or dedicated will allow you to do this. It's all handled through command line and only takes an hour or so to set up. DNS has to resolve just like normal domain propagation though.
     
    fourfingers, Apr 5, 2009 IP
  7. traian13

    traian13 Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I couldn't get this done on hostgator hosting... maybe i have to get the right tech support guy. I'll try again in a few days.
     
    traian13, Apr 6, 2009 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's not about VPS vs dedicated server, it's about whether or not you have access to make the required changes. The only pieces of the puzzle that require admin access are the change to httpd.conf and some DNS setup.
     
    SmallPotatoes, Apr 6, 2009 IP
  9. AveragePro

    AveragePro Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Why not just have a .htaccess forward a subdomain request to directory, such as sub.domain.com > domain.com/sub. Or even also mask the address to keep the subdomain.
     
    AveragePro, Apr 6, 2009 IP
  10. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #10
    thnx for this usefull info :)

    regards
     
    ghprod, Apr 12, 2009 IP