欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-15 11:42:19
1.主要文件,访问该页面,该页面根据“验证页面”的返回结果设置本文件的返回状态 header('http/1.1 '.$code.' '.$_status[$code])复...

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",