Watch web page for changes

Submitted by:Jhon Jhon

Date added:09 February, 2013

Category:PHP

There are websites that will watch a page for you and notify but I didn't really like the way they worked.

Tags: watch web page

Code Snippet:

$hostname_changes = "your_mysql_host";

// set up connection
$database_changes = "db_name";
$username_changes = "db_username";
$password_changes = "db_password";
$changes = mysql_pconnect($hostname_changes, $username_changes, $password_changes) or die("<p>This site is experiencing some technical difficulties. Please try again shortly, we
apologise for any delays\n</p>");

mysql_select_db($database_changes, $changes);
@$data = file_get_contents('URL_OF_PAGE');

//In my case I only wanted to compare a chunk of the page
//$length = strlen($data);
//$leftend = substr($data,0, 4362);
//$rightend = substr($data, $length-1265);
//$goodbit=substr($data, 4363, $length-1265-4363);

$goodbit=$data;

//grab the last page content
//database has 1 row, 2 columns, id (val=1) and sitecontent
$strSQL = "SELECT * FROM sitecontent";
$content = mysql_query($strSQL, $changes) or die(mysql_error());
$row_content = mysql_fetch_assoc($content);

//compare old content to new page
if($row_content["content"] != $goodbit){

//use phpmailer to send email
require("includes/class.phpmailer.php");
$mail = new PHPMailer();

$theBody = "<p>The page I am watching has changed</p><p>New Content:<br>$goodbit</p>";

$mail->Subject = ("Page Change");
$mail->AddAddress("info@yourdomain.com");
$mail->From = "info@yourdomain.com";
$mail->FromName = ("Website");
$mail->Body = $theBody;
$mail->AltBody = strip_tags($theBody);
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}


$strSQL = "UPDATE sitecontent SET content='" . addslashes($goodbit) . "' WHERE id=1";
mysql_query($strSQL, $changes) or die(mysql_error());

}
 
 

Comments