Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Database and User Created Via Script Issue

Discussion in 'Database Discussion' started by Tresford, Jul 8, 2018.

  1. Tresford

    Tresford Registered

    Joined:
    Sep 28, 2017
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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.
     
    #1 Tresford, Jul 8, 2018
    Last edited by a moderator: Jul 8, 2018
  2. 24x7server

    24x7server Well-Known Member

    Joined:
    Apr 17, 2013
    Messages:
    1,861
    Likes Received:
    89
    Trophy Points:
    78
    Location:
    India
    cPanel Access Level:
    Root Administrator
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. cPanelLauren

    cPanelLauren Forums Analyst
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    2,721
    Likes Received:
    186
    Trophy Points:
    143
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice