#native_company# #native_desc#
#native_cta#

Watch web page for changes

By Steve
on October 12, 2009

Version: adequate

Type: Full Script

Category: Other

License: GNU General Public License

Description: There are websites that will watch a page for you and notify but I didn’t really like the way they worked. I wrote my own. This should be set to run as a cron job. I run it every hour.

If you have any problems with the way I write code please email the to [email protected]

$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 delaysn</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("[email protected]");		
					$mail->From = "[email protected]";
					$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());

}