How to Create Subdomains and Ftp Accounts with PHP Develop by connecting to Cpanel tutorial



Hello every body, Today we will talk about how to create subdomains and ftp account with php by connecting to Cpanel

Let’s get started:

1- Create three php files as the following:

  1. cpanel.Config.php
  2. cpanel.functions.php
  3. cpanel.connection.php
2- open cpanel.Config.php and but the next code:

    $cpanel['sitename'] = 'yoursitename.com'; //Your domain name
    $cpanel['port'] = '2082'; // Cpanel port
    $cpanel['username'] = 'username'; //your cpanel username
    $cpanel['password'] = '123456'; //your cpanel password
    $cpanel['skin'] = 'x3'; //your cpanel skin name - x3 is always default

    ?>

This file is the configuration file , to set the Cpanel connection information

3- open cpanel.functions.php and type the following:


    function opencpanel($url)
    {
    global $cpanel; // get cpanel variable from config file
    $page = @file("http://".$cpanel['username'].":".$cpanel['password']."@www.".$cpanel['sitename'].":".$cpanel['port']."/frontend/".$cpanel['skin']."/".$url); // send username and password connection with the url path
    if($page) // if it done then return result
    {
    return implode("", $page);
    }
    else // else return false
    {
    return false;
    }
    }

    function create_ftp_account($username, $password, $ftp_quota)
    {
    $data = opencpanel("ftp/doaddftp.html?login=".$username."&password=".$password."&password2=".$password."&homedir=public_html/files/".$username.""a=".$ftp_quota); send information data to create the ftp to page (ftp/doaddftp.html) to create the FTP account
    if($data) // if return result
    {

    //get information
    $dstatu = explode('class="status">', $data);
    $dstatu2 = explode("", $dstatu[1]);
    if($dstatu2[0] == "Account created successfully.") //if this line =
    Account created successfully. then return it True
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }

    function create_subdomain($username)
    {
    $data = opencpanel("subdomain/doadddomain.html?domain=".$username."&rootdomain=".$cpanel['sitename']."&dir=public_html/");
    if($data)
    {
    $dstatu = explode('class="status">', $data);
    $dstatu2 = explode("\n", $dstatu[1]);
    if($dstatu2[0] == $username.".".$cpanel['sitename']." has been created!")
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }

    ?>


Now what is these functions ?

- opencpanel(here is the link of cpanel page) ->> this function for send data to cpanel and return the result by function file() with creating the connection (send username and password) ...

- create_subdomain (the subdomain name as you want)->> for creating the subdomain

create_ftp_account ->> to create the ftp accounts

4- Now open cpanel.connection.php file and the following :


    //include files

    include "cpanel.Config.php";

    include "cpanel.functions.php";

    //now if user want to create subdomain then it will appear here the $_POST['subdomainname'] here:

    if($_POST['subdomainname'])

    {

    $create = create_subdomain($_POST['subdomainname']);//create the sub domain

    if($create)

    {

    echo "Your subdomain ".$_POST['subdomainname']." Created successfully !
    "; // if return true then say created successfully

    }

    else

    {

    echo "Can't create the subdomain ".$_POST['subdomainname']." !
    "; // if not then say error

    }

    }

    //also if he want to create ftp account

    if($_POST['client_user_name'])

    {

    $create = create_ftp_account($_POST['client_user_name']$_POST['password'], "100");

    if($create)

    {

    echo "Your FTP ".$_POST['client_user_name']." Created successfully !
    "; // if return true then say created successfully

    }

    else

    {

    echo "Can't create the FTP ".$_POST['client_user_name']." !
    "; // if not then say error

    }

    }

    //here is the HTML page

    ?>

    Create new subdomain

    subdomain name:  


    Create new FTP account

    FTP username:





Now it's done,

you can test script, run: www.yoursite.com/cpanel.connection.php

Have fun

to download the tutorial from Attachment

Good luck



 Download 


Related Products