1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I create subdomains using PHP?

Discussion in 'Programming' started by mudanoman, Jan 25, 2006.

  1. #1
    I would like to know how do I create subdomains using php?

    Basically, I want a new user that signs up to my website to have a homepage: username.domain.com
    as well as sub directories as: username.domain.com/journal

    Thanks in advance,

    Ivan
     
    mudanoman, Jan 25, 2006 IP
  2. T-Soul

    T-Soul Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want subdomains for them to have free hosting or just as a redirect domain?
     
    T-Soul, Jan 25, 2006 IP
  3. neterslandreau

    neterslandreau Peon

    Messages:
    279
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you may be talking about a virtual domain in which case you need to create a new DNS entry in the named file as well as a new VirtualHost container in the httpd.conf file.
     
    neterslandreau, Jan 27, 2006 IP
  4. tccoder

    tccoder Peon

    Messages:
    69
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How do I create subdomains?

    Everyone has a "www" subdomain (In "www.yourdomain.com", the "www" part is the subdomain). The www subdomain is created by, and serves files from, your /htdocs/www directory on the filesystem.

    To add more subdomains, just create new subdirectories in the /htdocs directory.

    For instance, if you had the domain name mychevycars.com and wanted a subdomain for Corvettes, then you would just create the directory /htdocs/corvettes and upload the files in there that you wanted to see at http://corvettes.mychevycars.com

    Second Level Subdomains
    To create second level subdomains like www.corvettes.mychevycars.com ("www" is the second level subdomain and "corvettes" is the first level subdomain in this example) then you would do one of the following:

    1. If you have made an /htdocs/corvettes directory so that you have http://corvettes.mychevycars.com then you can make http://www.corvettes.mychevycars.com by getting a shell prompt and creating a symlink like so:

    cd /htdocs
    ln -s corvettes www.corvettes

    Now http://www.corvettes.mychevycars.com will lead to the same place as http://corvettes.mychevycars.com and all the files for both of these URLs are uploaded in the /htdocs/corvettes directory.

    2. Alternatively, you may want your second level subdomain to lead to a separate area of your website instead of being symlinked together with the first level subdomain. For example you may want http://splitbumper.corvettes.mychevycars.com (where "splitbumper" is the second level subdomain) to lead to a section of your website dedicated to 1963-1965 Corvettes with split bumpers, and have http://corvettes.mychevycars.com just lead to a different section of your site about Corvettes in general. To setup the second level subdomain this way, you would make a separate directory altogether for it, like this:

    cd /htdocs
    mkdir splitbumper.corvettes

    The "splitbumper.corvettes" subdirectory is created right alongside all the other subdomain subdirectories, including the "corvettes" subdirectory, if you made one. Then you upload files related to splitbumpers into the /htdocs/splitbumper.corvettes subdirectory, and view them in a browser at http://splitbumper.corvettes.mychevycars.com

    User-Contributed Notes
    add a note
    06-Mar-2003 12:27

    If you need to delete a subdomain symlink made with the "ln -s ..."

    rm -f /htdocs/www.corvettes


    08-Oct-2003 09:30

    Make sure the directory name you create is all lower-case, with no
    spaces. CAPS or Mixed-Case will not work.



    07-Nov-2003 21:09

    Careful that you only use letters, numbers and (I think) hyphens in your
    sub-domains. I spent a whole day debugging and wondering why PHP
    sessions weren't carrying through page to page. It was because I had an
    underscore in the subdomain. As soon as I changed the subdomain name not
    to have an underscore, then PHP sessions worked again and I could login
    to my application.
     
    tccoder, Jan 28, 2006 IP
  5. ottodo

    ottodo Guest

    Messages:
    2,055
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How can I manage this from my Cpanel??
     
    ottodo, Aug 13, 2006 IP
  6. fordP

    fordP Peon

    Messages:
    548
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #6
    To simply create subdomains from cpanel, click cpanel's subdomain icon

    Note that you need shell access to create the second level subdomains as tccoder said.


    I think it would be impossible to describe to you in detail how to implement automated creation of subdomains and it work with your implementation. You will need to make a script that will create a folder in /htdocs and then save their files into that subdomain. How exactly you do that is up to you
     
    fordP, Aug 13, 2006 IP
  7. neel_basu

    neel_basu Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hello Would You Please Tell me
    =======================
    I am Using Apache Server In My PC
    And Its http://localhost
    How Can I Use http://www.localhost/ To Be Redirected To http://localhost/www/
    Editing conf Files or Doing Something Else
     
    neel_basu, Dec 18, 2006 IP
  8. CuteAbhi

    CuteAbhi Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Create host port ownername password and request variables and call this function :)

    
    $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd";
    function subd($host,$port,$ownername,$passw,$request) {
    
      $sock = fsockopen('localhost',2082);
      if(!$sock) {
        print('Socket error');
        exit();
      }
    
      $authstr = "$ownername:$passw";
      $pass = base64_encode($authstr);
      $in = "GET $request\r\n";
      $in .= "HTTP/1.0\r\n";
      $in .= "Host:$host\r\n";
      $in .= "Authorization: Basic $pass\r\n";
      $in .= "\r\n";
     
      fputs($sock, $in);
      while (!feof($sock)) {
        $result .= fgets ($sock,128);
      }
      fclose( $sock );
    
      return $result;
    }
    
    foreach($doms as $dom) {
      $lines = explode(';',$dom);
      if (count($lines) == 2) {
        // domain and subdomain passed
        $domain = trim($lines[0]);
        $subd = trim($lines[1]);
      }
      else {
        // only subdomain passed
        $domain = getVar('domain', DOMAIN);
        $subd = trim($lines[0]);
      }
      // http://[domainhere]:2082/frontend/x/subdomain/doadddomain.html?domain=[subdomain here]&rootdomain=[domain here]
      $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd";
      $result = subd('localhost',2082,$cpaneluser,$cpanelpass,$request);
      $show = strip_tags($result);
      echo $show;
    }
    
    Code (markup):
     
    CuteAbhi, Jul 13, 2009 IP
  9. omtechnologies

    omtechnologies Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi,

    In case you have a cpanel based account, then you can easily create subdomain using php. Here is the link that shows how to create subdomain using PHP.

    http://php-tutor.com/creating-subdomains-automatically/

    This site has more quick php tutorials to various other whm and cpanel functionalies so you can check them out too...

    --
    Om Technologies:cool:
    The best web development php company
     
    omtechnologies, Aug 27, 2010 IP
  10. tazogamer

    tazogamer Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    The link above (for subdomain creator script) doesnt work.
    The new php script is here - zubrag.com/scripts/cpanel-subdomains-creator.php
     
    tazogamer, Mar 28, 2013 IP
  11. amol9supe

    amol9supe Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #12
    //Function to create subdomain
    function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain) {

    //Generate URL for access the subdomain creation in cPanel through PHP
    $buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/" . $subDomain;

    //Open the socket
    $openSocket = fsockopen('localhost',2082);
    if(!$openSocket) {
    //SHow error
    return "Socket error";
    exit();
    }

    //Login Details
    $authString = $cPanelUser . ":" . $cPanelPass;

    //Encrypt the Login Details
    $authPass = base64_encode($authString);

    //Request to Server using GET method
    $buildHeaders = "GET " . $buildRequest ."\r\n";

    //HTTP
    $buildHeaders .= "HTTP/1.0\r\n";
    //Define Host
    $buildHeaders .= "Host:localhost\r\n";

    //Request Authorization
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    //fputs
    fputs($openSocket, $buildHeaders);
    while(!feof($openSocket)) {
    fgets($openSocket,128);
    }
    fclose($openSocket);

    //Return the New SUbdomain with full URL
    $newDomain = "http://" . $subDomain . "." . $rootDomain . "/";

    //return with Message
    return "Created subdomain".$newDomain;

    }
    1. Call the subdomain creator function
    2. echo create_subdomain($subDomainname,$cPanelUserName,$cPanelPassName
     
    amol9supe, Jul 30, 2014 IP
  12. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #12

    Thank god you answered this from Jan 25, 2006.. now the original poster can finish his 8 year old project.
     
    NetStar, Aug 3, 2014 IP
    Scriptona likes this.