Add a new cpanel user from php file shell

To add a new cPanel user from a PHP file using shell commands, you can use the following code:

<?php

// Set the username and password for the new user
$username = 'newuser';
$password = 'newpassword';

// Set the shell command to add the new user
$shellCommand = "useradd -m -g users $username && echo \"$password\" | passwd --stdin $username";

// Execute the shell command using the exec function
exec($shellCommand, $output, $returnVar);

// Check if the command was successful
if ($returnVar == 0) {
    echo "New user $username created successfully!";
} else {
    echo "Error creating new user: $output";
}

?>

This code uses the useradd command to create a new user, and the passwd command to set the password for the new user. The exec function is used to execute the shell command, and the $output variable is used to capture the output of the command.

You can also use the cPanel API to add a new user, it's more secure and recommended way to do it.

<?php

// Set the username and password for the new user
$username = 'newuser';
$password = 'newpassword';

// Set the API username and password
$apiUsername = 'your_api_username';
$apiPassword = 'your_api_password';

// Set the API endpoint
$apiEndpoint = 'https://yourdomain.com:2087/xml-api/';

// Set the API function to call
$apiFunction = 'adduser';

// Set the API parameters
$apiParams = array(
    'user' => $username,
    'password' => $password,
    'email' => '[email protected]',
    'plan' => 'default'
);

// Create the API request
$apiRequest = $apiEndpoint. $apiFunction. '?user='. $apiUsername. '&pass='. $apiPassword;

// Loop through the API parameters and add them to the request
foreach ($apiParams as $key => $value) {
    $apiRequest.= '&'. $key. '='. $value;
}

// Send the API request
$ch = curl_init($apiRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// Check if the API request was successful
if (strpos($response, 'OK')!== false) {
    echo "New user $username created successfully!";
} else {
    echo "Error creating new user: $response";
}

?>

Make sure to replace the placeholders with your actual cPanel API username, password, and domain.

Also, you can use the WHM API to add a new user, it's more secure and recommended way to do it.

<?php

// Set the username and password for the new user
$username = 'newuser';
$password = 'newpassword';

// Set the API username and password
$apiUsername = 'your_api_username';
$apiPassword = 'your_api_password';

// Set the API endpoint
$apiEndpoint = 'https://yourdomain.com:2087/xml-api/';

// Set the API function to call
$apiFunction = 'add_user';

// Set the API parameters
$apiParams = array(
    'user' => $username,
    'password' => $password,
    'email' => '[email protected]',
    'plan' => 'default'
);

// Create the API request
$apiRequest = $apiEndpoint. $apiFunction. '?api.version=1&api.user='. $apiUsername. '&api.pass='. $apiPassword;

// Loop through the API parameters and add them to the request
foreach ($apiParams as $key => $value) {
    $apiRequest.= '&'. $key. '='. $value;
}

// Send the API request
$ch = curl_init($apiRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// Check if the API request was successful
if (strpos($response, 'OK')!== false) {
    echo "New user $username created successfully!";
} else {
    echo "Error creating new user: $response";
}

?>

Make sure to replace the placeholders with your actual WHM API username, password, and domain.

Please note that you need to have the necessary permissions and access to the cPanel API and WHM API to use these examples.