17 May, 2012

Server uptime monitoring

For a while now I had wanted to monitor in real time my web server's availability.  But how?  Anything on the server itself would fail - because, if the server was down, the script would not work. After the event, the logs would show what's happened of course; but I wanted instant notification of a problem. Lots of companies offer this service at a price.  I wanted it free!  The solution turns out to be quite simple.  Sign up to a free web server service.  I picked http://www.000webhost.com/ and created a script on that machine that runs every five minutes to look up a small file on my main server.  If it fails to find the file, the script sends me an alert email.  This does generate the occasional 'false negative' but that is better than the main site being down for hours unbeknown to me.  If the script is successful this is recorded on a database.  Once a day the script records the stats for day, purges the database table and sends me a summary.  I think that is quite neat...

20 January, 2012

Form submission with alert/confirm

This has been a perennial issue for me. I'd have a form with multiple submit buttons; and on one or more - but not all - of the submit buttons I would want a confirm dialogue. 'Save' or 'Amend' might go straight through but 'Delete' would be intercepted for a confirmation. I used to put a section in the form response page setting up another form with a confirm button. But I think I have found a more elegant solution, below (though it does rely on JavaScript being enabled).

In the form add a hidden field and submit button as follows:
<input type="hidden" name="confirm_delete" id="confirm_delete" value="0" />

<input type="submit" name="delete" value="Delete" onClick="confirmDelete(this.form);" />

Add a JavaScript function like this:
function confirmDelete(form) {
var answer = confirm("Are you sure you want to deleted this?");
if (answer == true) {
form.confirm_delete.value = 1;
form.submit();
}
}
Then you can handle the confirmed deletion by testing the hidden field
if($_POST['confirm_delete'] == 1){
// do whatever needs doing
}