Create database programatically from PHP on WHM/cPanel server

后端 未结 4 902
执笔经年
执笔经年 2021-01-07 04:29

I was wondering whether it is possible to create a new database and user, from PHP, on an WHM/cPanel server.

4条回答
  •  逝去的感伤
    2021-01-07 05:11

    For CPanel:

    function create_db($cPanelUser,$cPanelPass,$dbName) {
    
    $buildRequest = "/frontend/x3/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);
    
    
    //echo "Created database $dbName";
    
    }
    
    create_db('username','password','dbName');
    

提交回复
热议问题