php post 当前页,php 模拟post_验证页面的返回状态(实例讲解)

本文档介绍了一个PHP脚本,用于定期检测一组URL的HTTP状态码,一旦发现非200状态,发送邮件通知,并提供详细的响应状态信息。通过ini_set优化执行时间,涉及函数如GetStatusCode获取状态码,ResetUrl重置URL,SendResultMail发送邮件。
摘要由CSDN通过智能技术生成

ini_set('max_execution_time', 120);

include("CheckConfig.php");

function send_http_status($code) {

static $_status = array(

// Informational 1xx

=> 'Continue',

=> 'Switching Protocols',

// Success 2xx

=> 'OK',

=> 'Created',

=> 'Accepted',

=> 'Non-Authoritative Information',

=> 'No Content',

=> 'Reset Content',

=> 'Partial Content',

// Redirection 3xx

=> 'Multiple Choices',

=> 'Moved Permanently',

=> 'Moved Temporarily ',  // 1.1

=> 'See Other',

=> 'Not Modified',

=> 'Use Proxy',

// 306 is deprecated but reserved

=> 'Temporary Redirect',

// Client Error 4xx

=> 'Bad Request',

=> 'Unauthorized',

=> 'Payment Required',

=> 'Forbidden',

=> 'Not Found',

=> 'Method Not Allowed',

=> 'Not Acceptable',

=> 'Proxy Authentication Required',

=> 'Request Timeout',

=> 'Conflict',

=> 'Gone',

=> 'Length Required',

=> 'Precondition Failed',

=> 'Request Entity Too Large',

=> 'Request-URI Too Long',

=> 'Unsupported Media Type',

=> 'Requested Range Not Satisfiable',

=> 'Expectation Failed',

// Server Error 5xx

=> 'Internal Server Error',

=> 'Not Implemented',

=> 'Bad Gateway',

=> 'Service Unavailable',

=> 'Gateway Timeout',

=> 'HTTP Version Not Supported',

=> 'Bandwidth Limit Exceeded'

);

if(array_key_exists($code,$_status)) {

header('HTTP/1.1 '.$code.' '.$_status[$code]);

}

}

function GetStatusCode($url)

{

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url); //设置URL

curl_setopt($curl, CURLOPT_HEADER, 1); //获取Header

curl_setopt($curl,CURLOPT_NOBODY,true); //Body就不要了吧,我们只是需要Head

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //数据存到成字符串吧,别给我直接输出到屏幕了

$data = curl_exec($curl); //开始执行啦~

$HttpCode =curl_getinfo($curl,CURLINFO_HTTP_CODE); //我知道HTTPSTAT码哦~

curl_close($curl); //用完记得关掉他

return $HttpCode;

}

function ResetUrl($url)

{

if(strpos($url,"?")>0)

$url.="&rnd";

else

$url.="?rnd";

$url.=rand();

return $url;

}

function ShowStateInfo($UrlArr,$MailPara)

{

$count=count($UrlArr);

if(isset($_REQUEST["start"]))

{

$start=$_REQUEST["start"]*1;

}

else

{

$start=1;

}

if(isset($_REQUEST["end"]))

{

$end=$_REQUEST["end"]*1;

}

else

{

$end=$start;

}

$start=$start-1;

$end=$end-1;

if($start<0)

{

$start=0;

}

if($start>=0 && $start

{

if($end>=$count)

{

$end=$count-1;

}

if($end

{

$end=$start;

}

$sTime=date("Y/m/d H:m:s");

echo "开始时间".$sTime."
";

echo "检测结果
";

for($i=$start;$i<=$end;$i++)

{

$url=ResetUrl($UrlArr[$i]);

$state=GetStatusCode($url);

echo "  ".$state ." => ".$url."";

if($state!="200")

{

echo " 本条访问出错!
";

send_http_status($state);

//发邮件

require("Mail.php");

$MailPara["Subject"]="网站监控结果";

$MailPara["Body"]="错误信息:状态->".$state."
地址:".$url;

SendResultMail($MailPara);

break;

}

echo "
";

}

$eTime=date("Y/m/d H:m:s");

echo "结束时间".$eTime."
";

}

}

ShowStateInfo($UrlArr,$MailPara);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值