Skip to content

Instantly share code, notes, and snippets.

@oelna
Created March 4, 2020 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oelna/7f4816d02160c56e486182983e6f1fd0 to your computer and use it in GitHub Desktop.
Save oelna/7f4816d02160c56e486182983e6f1fd0 to your computer and use it in GitHub Desktop.
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
Copy link

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