Linux Blog

PHP Script To Log Into cPanel

Filed under: Shell Script Sundays — at 3:47 am on Sunday, February 24, 2008

Earlier this week I made a script that logs into cPanel to check statistics. Basically if you have a webhost that runs cPanel and you wish to log into cPanel for some reason then this script is for you. Once you are logged in you can basically do anything you would want to do. For example my specific use was to log into my cPanel nightly and parse some data provided by AWStats. But with some modification this script could automate anything you can do by hand.

Since this is more of a web project for me I decided to write my cPanel login script in PHP. I found a PHP class to login here. curl is used to fetch the URL’s and I parse the data using PCRE regular expressions. The statistics code is still very basic but I thought I would post it for those interested and what better place then The Linux Blog’s Shell Script Sundays column?

Onto the script.

It consists of three scripts each with their own purpose in run time. They are as follows:

cPanel.php – This script does all of the dirty work in connecting to cPanel and fetching the pages. I modified this from the original a little
class.mysql.php – Just a generic data base handler. MySQL configuration information is stored in here.
login.php – This is the script that starts off the process. I named it login.php instead of index.php so that I do not have it run as the default page in my web browser. login.php also does all of the parsing of the data and is where the data gets inserted into the database.

To run the script edit login.php and then you can either put it in your PHP powered web server directory or run it from the command line by doing:

php login.php

The output should be as follows:

Num: 0 Date: 2454521 uniques: X visits: X visits per visitor: (Xvisits/visitor) pages: X pages per visitor: (XPages/Visit) hits: X hits per visitor: (XHits/Visit) bandwidth: X GB bandwidth per visitor: (XMB/Visit)

Feel free to modify this as you wish. If any questions can be answered I’d be happy to do so. I’d like to hear what people are using this for too, so drop a comment!

Download the PHP cPanel Login Script

Man Pages for commands in this post »




Comment by David Grega

February 25, 2008 @ 4:41 pm

I just thought I’d give a head’s up that for some functionality that is part of cPanel itself, rather than third party stats programs integrated into cPanel, you may wish to use our APIs. The XML-API allows you to call API1 and API2 functions as well.

Keep in mind the X3 theme is essentially just a graphical user interface to the appropriate API1 and API2 functions. If you have root access to a machine running cPanel/WHM, you can look at /usr/local/cpanel/base/frontend/x3 to look at the code X3 uses so you can get a feel for the APIs. This will then allow you to essentially build your own external interface to cPanel/WHM functionality via the XML-API.

Some documentation for this is available at This documentation is still a work in progress as of writing this post.


Comment by Owen

February 26, 2008 @ 1:24 am

Hi David,

Would like to thank you for taking the time comment and give me a heads up! I was totally unaware of this. This seems like something that I could use and it wouldn’t be as much of a hack as this.

I’ll have to look into the WHM portion of it to see if it will allow me to do what I need to do.

Comment by rhui

September 21, 2008 @ 10:33 pm

hi there,

I’m also using cpanel and found this blog closely related to my problem (hopefully ^_^). Anyway, im trying to connect to my databases in cpanel using php scripts..unfortunately, the server always gives me this error:

“Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘user’@’’ (using password: YES) in /home/fusionw/public_html/reg.php on line 8
Error connecting to mysqlAccess denied for user ‘user’@’’ (using password: YES)”

i also downloaded your php script to log into cpanel …but i haven’t used it yet..can you please help me with this problem..? I’ll greatly appreciate it.


Comment by

September 22, 2008 @ 8:49 pm

The user name or password is incorrect for the host you are trying to connect to. You shouldn’t need to log into cpanel to connect to mySQL, there is an option for “Remote Access” that you can check to allow external hosts access to port 3306. Give that a shot.

Comment by Global UnionShops Group

December 27, 2010 @ 6:29 am

PHP cPanel Script

Hi! PHPcPanel is an opensource tool for automatically manage cPanel without requiring manually login.

It can automatically manage your cpane for creating FTP and mysql accounts. If you are a web master, you could easily combine PHPcPanel to your projects. Enjoy!
(1) Automatically login your cpanel without goto cpanel;

(2) Create/Edit ftp account by using PHP function;

(3) Create/Edit mysql account by using PHP function;

(4) It is easily combine PHPcPanel to your own php project to manage your cPanel..

Demo >>

Download Latest Version

Latest PHPcPanel v1.0.2

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>