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


Posted in PHP onOctober 28, 2013

1.主要文件,访问该页面,该页面根据“验证页面”的返回结果设置本文件的返回状态 header('HTTP/1.1 '.$code.' '.$_status[$code])

<?php
    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<$count)
        {
            if($end>=$count)
            {
                $end=$count-1;
            }
            if($end<$start)
            {
                $end=$start;
            }
            $sTime=date("Y/m/d H:m:s");
            echo "开始时间".$sTime."<br/>";
            echo "检测结果<br />";
            for($i=$start;$i<=$end;$i++)
            {
                $url=ResetUrl($UrlArr[$i]);
                $state=GetStatusCode($url);
                echo "  ".$state ." => <a href='http://".$url."' target='_blank'>".$url."<a>";
                if($state!="200")
                {
                    echo " <span style='color:red;font-weight:bold'>本条访问出错!</span><br/>";
                    send_http_status($state);
                    //发邮件
                    require("Mail.php");
                    $MailPara["Subject"]="网站监控结果";
                    $MailPara["Body"]="错误信息:状态-><span style='color:red;font-weight:bold'>".$state."</span><br/>地址:".$url;
                    SendResultMail($MailPara);
                    break;
                }
                echo "<br/>";
            }
            $eTime=date("Y/m/d H:m:s");
            echo "结束时间".$eTime."<br/>";
        }
    }
    ShowStateInfo($UrlArr,$MailPara);
?>

2.邮件
function SendResultMail($MailPara)
    {
        require("phpmailer/class.phpmailer.php"); 
        $mail = new PHPMailer(); 
        $mail->CharSet = $MailPara["CharSet"];
        $mail->IsSMTP();
        $mail->Host = $MailPara["Host"]; 
        $mail->Port = $MailPara["Port"];
        $mail->SMTPAuth = true; 
        $mail->Username = $MailPara["FromMail"];
        $mail->Password = $MailPara["FromMailPassword"];
        $mail->From = $MailPara["FromMail"]; 
        $mail->FromName = $MailPara["FromMailName"];
        foreach($MailPara["To"] as $toMail)
        {
            $mail->AddAddress($toMail["ToMail"], $toMail["ToMailName"]);
        }
        $mail->Subject = $MailPara["Subject"]; 
        $mail->Body = $MailPara["Body"]; 
        $mail->AltBody = $MailPara["AltBody"]; 
        if(!$mail->Send())
        {
            echo "邮件发送失败. <p>";
            echo "错误原因: " . $mail->ErrorInfo ."<br/>";
            exit;
        }
        echo "邮件发送成功<br/>";
    }

3.配置文件
<?php
    $UrlArr=array(
        "localhost/test/281892.shtml",
        "localhost/test/all-229-1-221.shtml",
        "localhost/testclass/all-254-1-1.shtml",
        "localhost/test/cheng/bd/1988478.html",
        "localhost/test/asd/2066495.html"
    );
    //邮箱发送相关信息
    $MailPara=array(
        "CharSet"=> "GB2312",
        "Host"=> "smtp.exmail.qq.com",            // 邮箱服务地址
        "Port"=>25,
        "FromMail"=> "fdsafdsafd@fdasfds.com",    // 发件人邮箱地址
        "FromMailPassword"=> "*********", // 发件人邮箱密码
        "FromMailName"=> "检测",            //发件人称呼
        "To"=>array(
            array(
                "ToMail"=>"defdafdsafdsafdf@qq.com",        //收件人邮箱地址
                "ToMailName"=> "bqq",            //收件人称呼
            ),
            array(
                "ToMail"=>"abfdsafdsafdsafc@gmail.com",        //收件人邮箱地址
                "ToMailName"=> "agmail",            //收件人称呼
            )
        ),
        "Subject"=> "",                //邮件标题
        "Body"=> "",            //邮件内容
        "AltBody"=> "附加信息"                //附加信息,可以省略        
    );
?>

邮件主要使用"phpmailer",点击下载
PHP 相关文章推荐
十天学会php之第一天
Oct 09 PHP
谈谈PHP语法(2)
Oct 09 PHP
PHP gbk环境下json_dencode传送来的汉字
Nov 13 PHP
Windows下安装PHP单元测试环境PHPUnit图文教程
Oct 24 PHP
PHPUnit安装及使用示例
Oct 29 PHP
windows中为php安装mongodb与memcache
Jan 06 PHP
jQuery+PHP发布的内容进行无刷新分页(Fckeditor)
Oct 22 PHP
PHP getNamespaces()函数讲解
Feb 03 PHP
php+Ajax无刷新验证用户名操作实例详解
Mar 04 PHP
YII框架学习笔记之命名空间、操作响应与视图操作示例
Apr 30 PHP
PHP PDO和消息队列的个人理解与应用实例分析
Nov 25 PHP
PhpSpreadsheet中文文档 | Spreadsheet操作教程实例
Apr 01 PHP
php操作mysqli(示例代码)
Oct 28 #PHP
php session_start()出错原因分析及解决方法
Oct 28 #PHP
php 强制下载文件实现代码
Oct 28 #PHP
php获取qq用户昵称和在线状态(实例分析)
Oct 27 #PHP
php获取数组长度的方法(有实例)
Oct 27 #PHP
使用淘宝IP库获取用户ip地理位置
Oct 27 #PHP
简单的php文件上传(实例)
Oct 27 #PHP
You might like
解析strtr函数的效率问题
2013/06/26 PHP
php过滤敏感词的示例
2014/03/31 PHP
Yii实现多按钮保存与提交的方法
2014/12/03 PHP
laravel 5 实现模板主题功能
2015/03/02 PHP
PHP实现Unicode编码相互转换的方法示例
2020/11/17 PHP
PHP 并发场景的几种解决方案
2019/06/14 PHP
在线游戏大家来找茬II
2006/09/30 Javascript
基于jquery的点击链接插入链接内容的代码
2012/07/31 Javascript
js中的this关键字详解
2013/09/25 Javascript
当滚动条滚动到页面底部自动加载增加内容的js代码
2014/05/13 Javascript
JS动态加载脚本并执行回调操作
2016/08/24 Javascript
bootstrap datepicker限定可选时间范围实现方法
2016/09/28 Javascript
AJAX和jQuery动态加载数据的实现方法
2016/12/05 Javascript
jQuery基本选择器和层次选择器学习使用
2017/02/27 Javascript
老生常谈JS中的继承及实现代码
2018/07/06 Javascript
AngularJS修改model值时,显示内容不变的实例
2018/09/13 Javascript
vue项目中使用scss的方法步骤
2019/05/16 Javascript
js中的this的指向问题详解
2019/08/29 Javascript
jquery绑定事件 bind和on的用法与区别分析
2020/05/22 jQuery
详解JS深拷贝与浅拷贝
2020/08/04 Javascript
[36:37]2014 DOTA2华西杯精英邀请赛5 24 VG VS iG
2014/05/25 DOTA
跟老齐学Python之字典,你还记得吗?
2014/09/20 Python
Python面向对象基础入门之设置对象属性
2018/12/11 Python
python使用html2text库实现从HTML转markdown的方法详解
2020/02/21 Python
Jupyter 无法下载文件夹如何实现曲线救国
2020/04/22 Python
HTML5 常用语法一览(列举不支持的属性)
2010/01/26 HTML / CSS
前台文员的岗位职责
2013/11/14 职场文书
护士辞职信范文
2014/01/19 职场文书
美容院店长岗位职责
2014/04/08 职场文书
企业承诺书格式
2014/05/21 职场文书
优秀研究生主要事迹
2014/06/03 职场文书
代领学位证书毕业证书委托书
2014/09/30 职场文书
中层干部考核评语
2015/01/04 职场文书
大学辅导员述职报告
2015/01/10 职场文书
晚会开幕词
2015/01/28 职场文书
党支部综合考察意见
2015/06/01 职场文书