How to create subdomains using php

matxanh335

Verified User
Joined
Jun 11, 2017
Messages
7
Hi
I want to create a subdomain on linux and host a DirectAdmin host using php.
Please write this file to make subdomain in my host by give the php file the name of my subdomain and it make it name to my host.
i am from vietnamese and my language not good sorry.
 
PHP:
$uname   	= "username";
$upass   	= "password";
$name   	= "".$uname."_db";
$user   	= "".$uname."_db";
$passwd 	= "pass";
$passwd2 	= "pass";


include 'configroot.php';
include 'protocol/httpsocket.php';
$Socket = new HTTPSocket;

$Socket->connect($siteurl,$siteport);
$Socket->set_login($uname,$upass);
// {admin_username} and {admin_password} are not specifically your administrative DA user,
// but rather the user that you want to own this new user.

$Socket->query('/CMD_API_DATABASES',
    array(
        'name' => $name, // The User's username. 4-8 characters, alphanumeric
        'user' => $user, // A valid email address
        'passwd' => $passwd, // The User's password. 5+ characters, ascii
        'passwd2' => $passwd2, // Password double check. 5+ characters, ascii
        'action' => 'create'
    ));
$result = $Socket->fetch_parsed_body();
echo '<pre>'; print_r( $result ); echo '</pre>';

if ( $Socket->get_status_code() != 200 || !empty($result['error']) )
{
    // failure.
    echo "Failed to create database: {$result['text']}<br />{$result['details']}";
} else {
    // new user added.
    print_r('Database '.$name.' created...');
}
 
Do you mean you already have an user (top domain), and now you want to create subdomain and database under that account?
 
You should have:

PHP:
$sock->query('/CMD_API_DATABASES',
        array(
                'action' => 'create',
                'name' => "db",
                'user' => "dbuser",
                'passwd' => "$pass",
                'passwd2' => "$pass",));

Username should not be included into 'name' and/or 'user' of DB. It will be handled by Directadmin.

Sample for creating DB: http://files.directadmin.com/services/all/httpsocket/examples/example.add_database.php
Other samples: http://files.directadmin.com/services/all/httpsocket/examples/

By the way here

PHP:
$uname       = "username"; 
$upass       = "password";

you should have credentials of an user, neither a reseller's nor admin's, unless you use "Login-As" feature (www.directadmin.com/api.html)

p.s. If it does not solve the issue, please specify What error you have?
 
You should have:

PHP:
$sock->query('/CMD_API_DATABASES',
        array(
                'action' => 'create',
                'name' => "db",
                'user' => "dbuser",
                'passwd' => "$pass",
                'passwd2' => "$pass",));

Username should not be included into 'name' and/or 'user' of DB. It will be handled by Directadmin.

Sample for creating DB: http://files.directadmin.com/services/all/httpsocket/examples/example.add_database.php
Other samples: http://files.directadmin.com/services/all/httpsocket/examples/

By the way here

PHP:
$uname       = "username"; 
$upass       = "password";

you should have credentials of an user, neither a reseller's nor admin's, unless you use "Login-As" feature (www.directadmin.com/api.html)

p.s. If it does not solve the issue, please specify What error you have?
It fixes my error successfully.
Thanks so much :)
 
You should have:

PHP:
$sock->query('/CMD_API_DATABASES',
        array(
                'action' => 'create',
                'name' => "db",
                'user' => "dbuser",
                'passwd' => "$pass",
                'passwd2' => "$pass",));

Username should not be included into 'name' and/or 'user' of DB. It will be handled by Directadmin.

Sample for creating DB: http://files.directadmin.com/services/all/httpsocket/examples/example.add_database.php
Other samples: http://files.directadmin.com/services/all/httpsocket/examples/

By the way here

PHP:
$uname       = "username"; 
$upass       = "password";

you should have credentials of an user, neither a reseller's nor admin's, unless you use "Login-As" feature (www.directadmin.com/api.html)

p.s. If it does not solve the issue, please specify What error you have?

It helped me fix the bug.
Thanks so much :)
 
Sorry to interrupt. Is there a way to restart litespeed in direct admin after creating user using direct admin?
Thanks
 
Back
Top