Monitoring your web site. Checking if the host server is running
PHP code to monitor your web site
By Sergey Skudaev
![]()
![]()
![]()
![]()
![]()
Here I show you how to monitor your web site and check how many times per day your host is getting down.
All error codes and description will be stored in the MySql database on your host control panel. First, we have to create a table to store error information. I called the table "mylog"
If you do not know how to create table in MySql database using phpMyAdmin read my tutorial about phpMyAdmin.
Here is script for our table:
CREATE TABLE IF NOT EXISTS mylog (
id int(11) NOT NULL auto_increment,
cerror varchar(100) NOT NULL,
errtime timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
We are going to use few PHP functions. The first one is curl_init() that initializes a new session and return a cURL handle. This cURL handle will be used with the curl_setopt(), curl_exec(), and curl_close() functions.
The bool curl_setopt ( resource $ch , int $option , mixed $value ) function sets URL options.
We will use this fuction to set two options. Detailed description of these options is located on www.PHP.net web site
CURLOPT_RETURNTRANSFER option is used to return only true or false from the curl_exec() function.
CURLOPT_CONNECTTIMEOUT option sets the number of seconds to wait while trying to connect.
The curl_exec(curl_handle) function executes the given cURL session.
The curl_error(curl_handle) function returns error text
The curl_getinfo(curl_handle) will be used to get http_code for errors
And curl_close(curl_handle) fucntion will close the cURL resurce.
Here is a list of http codes from www.php.net
The whole code goes below:
//get today date and time in "2009-03-19 17:15:10" format
$amp;today=date("Y-m-d H:m:s");
$amp;url='http://www.mydomain.com'; // the URL of your web site
$amp;timeout = 60; // the number of seconds to wait for request.
$amp;curl_handle = curl_init($amp;url);
curl_setopt($amp;curl_handle, CURLOPT_RETURNTRANSFER, 0); // return TRUE or FALSE
curl_setopt($amp;curl_handle, CURLOPT_CONNECTTIMEOUT, $amp;timeout);
ob_start(); //turn on buffer
$amp;result = curl_exec($amp;curl_handle);
ob_end_clean(); //clear buffer
$amp;cinfo = curl_getinfo($amp;curl_handle);
curl_close($amp;curl_handle);
if ( !$amp;result || $amp;cinfo['http_code'] != 200 )
{
$cerror =" HTTP Code: [". $cinfo['http_code']. "]";
//Returns a text error message.
if ( curl_error($curl_handle) )
$cerror .= " ". curl_error($curl_handle);
$amp;hostname = "localhost";
$amp;dbuser = "your_user";
$amp;dbpassword = "your_password";
$amp;dbname = "your_db";
$amp;db_link=mysql_connect($amp;host, $amp;dbuser, $amp;dbpassword) or die("Unable to connect to the server!");
mysql_select_db($amp;dbname) or die("Unable to connect to the database");
$amp;isql="insert into mylog(id, cerror, errtime) values(0, '".$amp;cerror."','".$amp;today."')";
if(!mysql_query($amp;isql))
{
echo mysql_errno() . ":error ";
echo mysql_error() . "<br/>";
}
}
?>
Now to monitor your web site you have to run that sript periodically, for example, every hour.
Your host control panel has cron job
that allows you to set schedule and run the script.
Double click this icon and on the next window select the Advance (Unix Style) button.

On the next window set up time to run the script. On the table time set to run script once a day.
| 0 | 0 | * | * | * |
On the image below time set to run script every hour.

In the command field enter path to PHP. Usually, it is '/usr/local/bin/php'
Then you have to enter the path to your script. You can fint it out on control panel. It is called "home directory.
![]()
If you cannot find home directory go to file manager
and double click the icon. You will see your home directory

If your script name is myscript.php and you put in the root directory, then your whole command will be like that:
"/usr/local/bin/php home/user/www/myscript.php"
It is better, however, to put the script in protected folder.
After entering command, click Commit Changes button. Your cron job is saved.First time you may try to run this script manually to see if you did not mistype something.
All error in cript can be found in Error Log
on your control panel
Then you may set time to nearest minute for example if now is 12:30 you can set cron table to 12:32 and save.
That is it
If you sign for hosting through one of 4 links on my site I placed below I will send you PHP authentication templates for free. See details user Authentication Demo
Hosting companies I use and recomend!
1. Fatcow.com I use it for www.configure-all.com, www.hardstuffez.com and www.healthstairs.com
40% off at FatCow!
2. www.bluehost.com
Host Unlimited Domains - 1 Account $6.95 Per Month
3. www.hostmonster.com
HostMonster - Perl, CGI, SSH, Free Domain
Affiliate Disclosure: I may get comission, when you purchase products or services through links on my website.
Did you find information useful?
Send to your friend a link to this page
If you like this page click +1 button.
Please rate the tutorial
How to Build Your Own Web Site from Scratch [Kindle Edition] $2.99
Earn Money on Internet as an Affiliate [Kindle Edition] $0.99
| Comments | |
|---|---|
- Home
- check_server
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- Java Code Examples
- Learn Java
- Java, MS Access, MySQL
- Java Random Generator
- Thread Applet
- Sort Algorithms
- Computer Game
- Poet Applet
- More Code Examples
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn PHP
- Learn C++
- Learn Visual Basic
- Web Site Tips
- User_Auth. Demo
- Using Twitter
- Advertise here
- Site map
- Registration
Web programming Tips