Monitor webpage authentication for text

Here is the deal. I have a requirement to track a specific password-protected webpage for changes and to play a sound when the page size is different from what I know (i.e. it has been resized). Here is a piece of code that I came up with.

<?php $e = curl_init(); curl_setopt($e, CURLOPT_URL, 'http://example.com/Account/LogOn'); curl_setopt($e, CURLOPT_POST, 1); curl_setopt($e, CURLOPT_POSTFIELDS, ' UserName=xxx@example.com &Password=password'); curl_setopt($e, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($e, CURLOPT_REFERER, 'http://example.com'); curl_setopt($e, CURLOPT_RETURNTRANSFER, 1); curl_exec($e); $sizevalue = 2399; do { curl_setopt($e, CURLOPT_URL, 'http://example.com/panel.php'); $content = curl_exec($e); $numberofchars = strlen($content); sleep(15); } while ($numberofchars = $sizevalue); curl_close($e); echo ' <audio controls="controls" autoplay="autoplay"> <source src="/path/to/your/audio.mp3" type="audio/mp3"> <source src="/path/to/your/audio.ogg" type="audio/ogg"> <embed src="/path/to/your/audio.mp3"> </audio>'; php?> 

Problem 1 . Correct me if I am wrong, but as I understand it, instead of connecting to the server, sending username and password only once, leaving the connection alive and using this auth key from cookie.txt for subsequent actions, it constantly sends the name user and password in the login form for each cycle run. How to fix it?

Problem 2 I need a script to give me some temporary status, because at the moment, if a certain condition is not met, it basically turns into an infinite loop, and the server hosting the script gives me a timeout error.

Problem 3 . What will be the easiest way to implement an audible alarm? OGG file playback? Youtube redirect?

+6
source share
1 answer

Problem 1 : curl_multi_exec not needed in your case. Storage of the curl handle should be sufficient.

Problem 2 : sleep() or usleep() works well for this purpose

Problem 3 . You can simply output the html markup. Another option is to embed a flash file or video that automatically plays an audio signal. Depends on where this is done and for what purpose.

Note: scripts like this are best run in a browser. It is better to set up a cron script that checks URLs at set intervals and does not use do while or sleep loops.

Here your code is written with these corrections and a few examples.

 <?php set_time_limit(0); // this script will now run forever. but a safer value might be something like X hours or X number of minutes. $e = curl_init(); curl_setopt($e, CURLOPT_URL, 'http://example.com/Account/LogOn'); curl_setopt($e, CURLOPT_POST, true); curl_setopt($e, CURLOPT_POSTFIELDS, ' UserName=xxx@example.com &Password=password'); // you'll need both CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE if you plan on NOT using curl internal cookie handling curl_setopt($e, CURLOPT_COOKIEJAR, 'cookie.txt'); // this is where we write the cookie data curl_setopt($e, CURLOPT_COOKIEFILE, 'cookie.txt'); // this is where we read the cookie data to use curl_setopt($e, CURLOPT_REFERER, 'http://example.com/'); curl_setopt($e, CURLOPT_RETURNTRANSFER, true); // you can omit the next two lines if you think they won't be needed curl_setopt($e, CURLOPT_FOLLOWLOCATION, true); // in case the remote server does some kind of http redirection, like a 301 redirect curl_setopt($e, CURLOPT_MAXREDIRS, 3); // The maximum amount of HTTP redirections to follow, in case the remote server is badly configured and has an endless redirection loop curl_exec($e); $sizevalue = 2399; $maximum_checks = 30; // I've added this as a cap for the loop, so that you only run it this many times. $checks = 0; $seconds = 15; do { $checks++; curl_setopt($e, CURLOPT_URL, 'http://example.com/panel.php'); $content = curl_exec($e); $numberofchars = strlen($content); // optionally... instead of the strlen method above you could also use // http://php.net/manual/en/function.curl-getinfo.php if (!curl_errno($e)) { $info = curl_getinfo($e); echo 'content-length of download, read from Content-Length: field :' . $info['download_content_length']."<br>\n"; $numberofchars = $info['download_content_length']; } if ($checks === $maximum_checks) { exit('No changes after '.($seconds * $maximum_checks).' seconds<br>'.PHP_EOL); } sleep($seconds); // wait for 15 seconds } while ($numberofchars != $sizevalue); // fix up your TRUTH expression, don't forget it is != for proper comparison in your case. Without it you go into an infinite loop. curl_close($e); // if our PHP  got this far then echo ' <audio controls="controls" autoplay="autoplay"> <source src="/path/to/your/audio.mp3" type="audio/mp3"> <source src="/path/to/your/audio.ogg" type="audio/ogg"> <embed src="/path/to/your/audio.mp3"> </audio>'; ?> 
+1
source

All Articles