views:

45

answers:

2

Is there a sort of reliable way to programmatically create MySQL databases and MySQL users on shared Linux hosting plans from a PHP page? For instance, accessing cpanel or phpmyadmin through an API or hook if I know my cpanel access information and/or my FTP information?

Occasionally I see this with some sites and products but don't know how they do it.

A: 

Giving shared user permissions to do anything outside of cPanel seems to miss the point of using cPanel at all, so I would suggest that they probably do not. Your cPanel access information and your FTP information have little to do with MySQL--cPanel uses a MySQL root-authorized user to add databases and users, and unless cPanel exposes an API to do it (I don't think it does, and I don't think anyone sane would enable it on their shared hosting if they did).

Ed Ropple
It's all about automation. If you're wanting to make a product that connects to someone's cpanel (with their permission and login info, mind you) and to automate some tasks for them. This is why it is used.
Volomike
+1  A: 

Thanks to @Alex C who got me on the right track. The following will work with many shared hosting plans, but you'll want to check with their policies on this first. Mine wrote me back in a tech support ticket and said, "You have to automate through a cpanel script." So, that's what this is.

In the example below, I would have already purchased root.com as my main root domain of my shared hosting plan. I would be wanting to setup a database named "user_myexample" with password "myexample" assigned with full privileges to user "user_myexamp". If the "user_" prefix and "myexamp" looks odd -- it's because cpanel has a prefix based on the root user's user account into cpanel, and the database user name can only be 7 characters maximum.

To connect to Cpanel to make this happen, I entered a cpanel homepage URL (which varies with hosting plan) so that it could be parsed and reused. Also, I provided my root.com's cpanel user/pass information.

The echo statements are just the output response whether each HTTP GET request worked or not. You may want to inspect that to see if there's something you can parse out of that output for success/failure.

Note some hosting plans block file_get_contents connecting to a URL, so you may have to switch with fopen($sURL, 'r') or Curl API.

<?php

// @ input vars
$sPastedCpanelHomepageURL = 'https://root.com:2083/frontend/x3/index.html';
$sNewDB = 'myexample';
$sNewDBUser = 'myexamp'; // must be 7 chars max
$sNewDBPass = 'myexample';
$sCPanelUser = 'user';
$sCPanelPass = 'pass';

// @ processing
$sCP = dirname($sPastedCpanelHomepageURL);
$sCP = str_replace('://','://' . $sCPanelUser . ':' . $sCPanelPass . '@',$sCP);

$sPrefix = substr($sCPanelUser, 0, 7) . '_';

$sTask1 = '/sql/addb.html?';
$sTask2 = '/sql/adduser.html?';
$sTask3 = '/sql/addusertodb.html?';

$sNewDB = urlencode($sNewDB);
$sNewDBUser = urlencode($sNewDBUser);
$sNewDBPass = urlencode($sNewDBPass);
$sCPanelUser = urlencode($sCPanelUser);
$sCPanelPass = urlencode($sCPanelPass);

$sNewDBUser = substr($sNewDBUser, 0, 7);

$asData1 = array(
  'db' => $sNewDB
);
$sData1 = http_build_query($asData1);
$s = file_get_contents($sCP . $sTask1 . $sData1);
echo "$s\n";

$asData2 = array(
  'user' => $sPrefix . $sNewDBUser,
  'pass' => $sNewDBPass,
  'pass2' => $sNewDBPass
);
$sData2 = http_build_query($asData2);
$s = file_get_contents($sCP . $sTask2 . $sData2);
echo "$s\n";

$asData3 = array(
  'user' => $sPrefix . $sNewDBUser,
  'db' => $sPrefix . $sNewDB,
  'update' => '',
  'ALL' => 'ALL'
);
$sData3 = http_build_query($asData3);
$s = file_get_contents($sCP . $sTask3 . $sData3);
echo "$s\n";
Volomike