这个脚本是为一个特定需求而编写的,我认为它可以帮助到很多人。它可以在您的任一网站无法正常运行时通过电子邮件向您发送警报(服务器没有响应,网站没有响应等)。
为此,当然需要在将该脚本放置在与其他站点分离的服务器上后为它创建一个 CRON(否则如果服务器崩溃,脚本本身将无法访问)。这个 CRON 例如每天执行 3 次(早上、中午和晚上),以保持快速响应。
以下是相关代码页面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php // Mot de passe de sécurité if( $_GET['mdp'] != 'sefUIHGDEjdpzjiOEanf' ) { exit; } $sitealerte = ''; // Fonction de test function testersite($url) { $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url ); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_VERBOSE,false); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_SSLVERSION,3); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false); $page = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if( $httpcode >= 200 && $httpcode < 300 ) { return true; } else { return false; } } // Sites à vérifier $site[1] = 'http://www.votresite.fr'; $site[2] = 'http://www.autresite.com'; // Emails à notifier $email[1] = 'votremail@gmail.com'; $email[2] = 'emailpro@unsite.fr'; $email[3] = 'encore-un@yahoo.fr'; // Boucle des vérifications for( $i = 1; $i <= count($site); $i++ ) { if(testersite($site[$i])) { echo $site[$i] . ' : Le site fonctionne.<br>'; } else { echo $site[$i] . ' : <strong>Le site ne fonctionne plus !</strong><br>'; $sitealerte = ( empty($sitealerte) ) ? $site[$i] : $sitealerte . "\n" . $site[$i]; } } // Boucle des emails if( !empty($sitealerte) ) { for( $j = 1; $j <= count($email); $j++ ) { mail($email[$j], 'ALERT SITE : Un ou plusieurs sites ne fonctionnent plus !', "Attention, le(s) site(s) Internet ci-dessous ne semble plus fonctionner :\n\n" . $sitealerte . " \n\nCeci est un mail automatique, veuillez ne pas y répondre.", 'From: ALERT SITE <no-reply@monmail.com>'); } } exit; ?> |
安全密码 « sefUIHGDEjdpzjiOEanf » 是一个示例,您当然需要修改它并创建一个新的。要在 CRON 调用您的页面,请记住在 URL 中包含这个密码: http://www.votresite.com/script.php?mdp=sefUIHGDEjdpzjiOEanf(示例)
网站和电子邮件列表可以根据需要多一些或少一些,自行选择。

Laisser un commentaire