Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect when a website changed by logging hashes of the content (eg. check for domain availability)
<?php
/*
Since this script works by detecting changes to the page source code
it works only on static pages. Sites that often change, eg. are generated
by a CMS or embed changing script filenames will always trigger.
There is no easy way around this limitation as far as I know.
Instructions:
- Add your Email and desired URLs
- Run the script periodically (I set up a cron job: 0 7 * * * /usr/bin/php ~/check_website.php)
*/
// settings
$notify_email = 'your.email@host.com';
$domains = array(
'http://domainnametowatch.com',
'https://somepage.net/dir/site.html'
);
// load existing data
$script_filename = basename(__FILE__, '.php');
$log_filename = __DIR__.DIRECTORY_SEPARATOR.$script_filename.'.json';
$log_file = '[]';
if(file_exists($log_filename)) {
$log_file = file_get_contents($log_filename);
}
$log_data = json_decode($log_file, true);
$now = time();
$messages = '';
foreach ($domains as $domain) {
$hash = hash_file('sha256', $domain);
$headers = get_headers($domain);
$found = false;
if(sizeof($log_data) > 0) {
foreach ($log_data as $log_item) {
if($log_item['domain'] == $domain) {
if($log_item['hash'] !== $hash) {
// site content changed
$messages .= 'The content of the page at '.$domain.' changed from '.$log_item['hash'].' to '.$hash.'.'.PHP_EOL;
}
if($log_item['status'] !== $headers[0]) {
// http status response changed
$messages .= 'The http status of the page at '.$domain.' changed from "'.$log_item['status'].'" to "'.$headers[0].'".'.PHP_EOL;
}
$found = true;
break;
}
}
}
if(!$found) {
$log_data[] = array(
'domain' => $domain,
'hash' => $hash,
'status' => $headers[0],
'last_check' => $now
);
}
}
// log results to file
$json_data = json_encode($log_data, JSON_PRETTY_PRINT);
file_put_contents($log_filename, $json_data);
// notify via email
if(strlen($messages) > 0 && !empty($notify_email)) {
@mail($notify_email, 'A watched website changed!', $messages);
}
@selimhex

This comment has been minimized.

Copy link

@selimhex selimhex commented Apr 19, 2020

Just a wild idea: How about initializing a local git and commit each change locally to detect changes?:) (through executing git-diff / git-status etc over php)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment