<?  
header("Cache-Control:no-cache"); 
//$self = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; 
$self = $_SERVER['PHP_SELF']; 
if(isset($_POST['downTime'])&&isset($_POST['upTime'])){ 
    if(is_numeric($_POST['downTime'])&&is_numeric($_POST['upTime'])){ 
        showResult(); 
    }else{ 
        showTest(); 
    } 
}elseif($_POST['action']=='uploadTest'){ 
    echo 'ok'; 
}else{ 
    showTest(); 
} 
function showTest(){?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
        <title>腾马PHP--网速测试程序  测试正在进行中……</title> 
    </head> 
    <body> 
        <table align="center" cellpadding="1" cellspacing="0"> 
            <tr> 
                <td height="30" colspan="2" align="center"><b>下载速度测试中</b></td> 
            </tr> 
            <tr> 
                <td width="300" style="border:1px solid blue"><table cellpadding="0" cellspacing="0"> 
                        <tr> 
                            <td id="downProgressBar" bgcolor="blue" style="width:0px; height:17px"></td> 
                        </tr> 
                    </table></td> 
                <td id="downProgressNum" align="right" width="35">0%</td> 
            </tr> 
            <form id="TestForm" action="" method="post"><input name="downTime" id="downTime" type="hidden" /><input name="upTime" id="upTime" type="hidden" /></form> 
        </table> 
        <script type="text/javascript"> 
            <!-- 
            function $id(id){ 
                return document.getElementById(id); 
            } 
  
            function setDownProgress(){ 
                Percentage++; 
                dpb.style.width=(Percentage*3)+'px'; 
                dpn.innerHTML = Percentage+'%'; 
            } 
  
            function setUpProgress(){ 
                Percentage+=10; 
                upb.style.width=(Percentage*3)+'px'; 
                upn.innerHTML = Percentage+'%'; 
                if(Percentage==100){ 
                    upTime =(new Date()).getTime() - upTime; 
                    submitTest(); 
                } 
            } 
  
            function XmlHttp(){ 
                try{ 
                    if(window.XMLHttpRequest){ 
                        var req = new XMLHttpRequest(); 
                        if(req.readyState == null){ 
                            req.readyState = 1; 
                            req.addEventListener("load", function(){ 
                                req.readyState = 4; 
                                if(typeof req.onreadystatechange == "function") 
                                    req.onreadystatechange(); 
                            }, false); 
                        } 
                        return req; 
                    } 
                    if(window.ActiveXObject){ 
                        return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp"); 
                    } 
                } 
                catch(e){} 
            } 
  
            function getXmlHttpPrefix(){ 
                if(getXmlHttpPrefix.prefix) 
                    return getXmlHttpPrefix.prefix; 
                var prefixes = ["MSXML2","Microsoft","MSXML","MSXML3"]; 
                var o; 
                for(var i=0;i<prefixes.length;i++){ 
                    try{ 
                        o = new ActiveXObject(prefixes[i] + ".XmlHttp"); 
                        return getXmlHttpPrefix.prefix = prefixes[i]; 
                    } 
                    catch(ex){}; 
                } 
            } 
  
            function upTest(){ 
                var xmlHttp = XmlHttp(); 
                if(!xmlHttp)errorSubmit(); 
                xmlHttp.open('POST','<?=$GLOBALS['self']?>',true); 
                xmlHttp.setRequestHeader("Content-Length",upBody.length); 
                xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded"); 
                xmlHttp.onreadystatechange = function(){ 
                    if(xmlHttp.readyState == 4){ 
                        setUpProgress(); 
                    }else if(xmlHttp.readyState == 3){ 
                        upTest(); 
                    } 
                } 
                xmlHttp.send(upBody); 
            } 
  
            function submitTest(){ 
                $id('downTime').value=downTime; 
                $id('upTime').value=upTime; 
                $id('TestForm').submit(); 
            } 
  
            function errorSubmit(t){ 
                if(t) 
                    upTime = t; 
                else 
                    upTime = 0; 
                submitTest(); 
            } 
  
            var dpb = $id("downProgressBar"); 
            var dpn = $id("downProgressNum"); 
            var upClickCount = Percentage = 0; 
            var downTime =(new Date()).getTime(); 
            --> 
        </script><?php 
            $outText = getTestText(); 
            for($i = 1;$i<100;$i++){ 
                echo '<!--'.$outText."-->\n"; 
                echo "<script type=\"text/javascript\">setDownProgress();</script>\n"; 
            } 
            ?> 
        <script type="text/javascript"> 
            var outstr = '<?=$outText?>'; 
            setDownProgress(); 
            downTime =(new Date()).getTime()-downTime; 
        </script> 
        <table align="center" cellpadding="1" cellspacing="0"> 
            <tr> 
                <td height="30" colspan="2" align="center"><b>上转速度测试中</b></td> 
            </tr> 
            <tr> 
                <td width="300" style="border:1px solid blue"><table cellpadding="0" cellspacing="0"> 
                        <tr> 
                            <td id="upProgressBar" bgcolor="blue" style="width:0px; height:17px"></td> 
                        </tr> 
                    </table></td> 
                <td id="upProgressNum" align="right" width="35">0%</td> 
            </tr> 
        </table> 
        <script type="text/javascript"> 
            Percentage =0; 
            var upb = $id("upProgressBar"); 
            var upn = $id("upProgressNum"); 
            var upBody=''; 
            for(i=0;i<10;i++)upBody +=outstr; 
            upBody = 'action=uploadTest&content='+upBody; 
            setTimeout('errorSubmit(-1)',300000); 
            upTest(); 
            var upTime =(new Date()).getTime(); 
        </script> 
    </body> 
</html><?php 
} 
function showResult(){?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
        <title>腾马PHP--测试结果</title> 
    </head> 
		<style type="text/css">
    @import url(/images/style.css);
    body { background:none;}
    .box { border:none; background:none; background:#F7FBFF;}
    #speed { width:800px; margin:0 auto; padding:20px 0; font-size:14px}
    #desc { width:400px; margin-top:65px; padding:15px;border:1px dashed #ace; background:url(/images/speed_icon.jpg) 15px 15px no-repeat #EDF6FF;}
    #desc p {padding-left:80px;}
    #result .con table { margin-top:0;}
    #result .con table, #result .con td,#result .con th { border:none;}
    #result .con th { border-bottom:1px solid #ace;}
    #result .con td,#result .con th { padding:0 8px; text-align:left; font-size:14px}
    #result .con td.stat,#result .con th.stat { width:25%; color:#666;}
    #result .con td.connect,#result .con th.connect { width:55%;}
    #result .con td.down, #result .con th.down{ width:20%; color:#666; text-align: right}
    #result .con th.down{ text-align:center;}
    #result .con th,#result .con th.stat ,#result .con th.down{ color:#000; font-weight:normal}
    .s { height:10px;overflow:hidden; background-image:url(/images/speed_bg.jpg); background-repeat:repeat-x; line-height:10px; float:left; margin-top:5px; margin-right:5px;}
    #s1,#s2,#s3,#s4,#s5,#s6,#s7,#s8,#s9,#s10{ height:20px; line-height:20px; font-size:12px; font-weight:bold}
    #s8 {color:#09f;}
    #s7,#s6 { color:#C0E91F;}
    #s7 .s,#s6 .s { background-color:#C0E91F;}
    #s9,#s10 { color:#2DDA30}
    #s9 .s ,#s10 .s{ background-color:#2DDA30}
    #s5,#s4,#s3 { color:#FC9B0C}
    #s5 .s,#s4 .s,#s3 .s{ background-color:#FC9B0C}
    #s2 { color:#FF6707}
    #s2 .s{ background-color:#FF6707}
    #s1 { color:#FB0A10}
    #s1 .s { background-color:#FB0A10}
    #s1 .s { width:1px;}
    #s2 .s{ width:4px; background-position:0 -10px;}
    #s3 .s { width:12px; background-position:0 -20px}
    #s4  .s{ width:17px; background-position:0 -30px}
    #s5  .s{ width:25px; background-position:0 -40px}
    #s6 .s { width:50px; background-position:0 -50px}
    #s7 .s { width:67px; background-position:0 -60px}
    #s8 .s { width:83px; background-position:0 -70px}
    #s9 .s { width:229px; background-position:0 -80px}
    #s10 .s { width:336px; background-position:0 -90px}
    </style>
    <body> 






	                               <div id="speed">
                <div id="info" class=" clearfix">
                    <div id="desc" class="fr">
                    <p>
                        建议您多测几次，取平均值，这样会比较接近真实值
    测试完成......
    您可以<a href="speed.php" class="red">&lt;&lt;重新测试&gt;&gt</a>，多测几次会较准确些O(∩_∩)O哈哈~	</p>
                    </div>

		            <div class="infocon">

                    <h5>测试点名称：小树芽网址导航</h5>
                    <h5>测试点地址：<a href="http://wz.xiaoshuya.cn">wz.xiaoshuya.cn</a></h5>
 		<br /><br /><br />      
                    </div>
		<br /><br /><br /><br />
		<div style="font-size:30px;">
		<?php 
            $dKBps = round(500000/$_POST['downTime'],2); 
            $dKbps = $dKBps * 8; 
            echo '下载速度：'.$dKbps.'Kbps，相当于'.$dKBps."KB/s<br />\n"; 
            if($_POST['upTime']=='-1'){ 
                echo '上转测试超时。'; 
            }else if($_POST['upTime']=='0'){ 
                    echo '您的浏览器不支持本上传测试程序，请使用IE6.0以上或FireFox。'; 
                }else{ 
                    $uKBps = round(500000/$_POST['upTime'],2); 
                    $uKbps = $uKBps * 8; 
                    echo '上传速度：'.$uKbps.'Kbps，相当于'.$uKBps.'KB/s'; 
                } 
            echo ''?> 
		 </div>
		 <br><br><br>
		<p style="text-align:center"><a href="speed.php" class="red">重新测试</a></p>
    </body> 
</html><?php 
} 
function getTestText(){ 
    $result; 
    for($i = 0;$i<5000;$i++){ 
        $result .= '*'; 
    } 
    return $result; 
} 
?> 