Database and User Created Via Script Issue

Tresford

Registered
Sep 28, 2017
1
0
1
Zambia
cPanel Access Level
Root Administrator
I am using the functions below to create a database and add a user to that database
Code:
function createDb($dbName) {
      
       $cPanelUser = 'user';
       $cPanelPass = 'password';

       $buildRequest = "/frontend/paper_lantern/sql/addb.html?db=".$dbName;

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

       $authString = $cPanelUser . ":" . $cPanelPass;
       $authPass = base64_encode($authString);
       $buildHeaders  = "GET " . $buildRequest ."\r\n";
       $buildHeaders .= "HTTP/1.0\r\n";
       $buildHeaders .= "Host:localhost\r\n";
       $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
       $buildHeaders .= "\r\n";

       fputs($openSocket, $buildHeaders);
       while(!feof($openSocket)) {
           fgets($openSocket,128);
       }
       fclose($openSocket);
      
   }
  
   function addUserToDb($dbName) {
      
       $cPanelUser = 'user';
       $cPanelPass = 'password';
       $userName = 'databaseuser'; //this user is already created

       $buildRequest = "/frontend/paper_lantern/sql/addusertodb.html?user=".$userName."&db=".$dbName.'&ALL=ALL';

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

       $authString = $cPanelUser . ":" . $cPanelPass;
       $authPass = base64_encode($authString);
       $buildHeaders  = "GET " . $buildRequest ."\r\n";
       $buildHeaders .= "HTTP/1.0\r\n";
       $buildHeaders .= "Host:localhost\r\n";
       $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
       $buildHeaders .= "\r\n";

       fputs($openSocket, $buildHeaders);
       while(!feof($openSocket)) {
           fgets($openSocket,128);
       }
       fclose($openSocket);
   }
After the script runs, i can confim on cpanel interface that the database has been created, and it shows the privileged user.
The problem is when I ran the app, it says user has no access to database.
When i remove the user and re add manually from cpanel, the app runs perfectly.
Why is it not working on the first case.
 
Last edited by a moderator: