用PHP监控网站在线状态

<?php
/**
*
* @param string $url
* @return boolean
*/
function SiteMonitorBot($url) {

$agent = ‘Mozilla/5.0 (compatible; SiteMonitorBot/1.0; +http://www.xiaokyun.com)’;

$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_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

$content = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

//echo $http_code.'<br>’;

if (($http_code >= 200 && $http_code <= 300) // (!empty($content) &&
|| $http_code == 301 || $http_code == 302 || $http_code == 307) { //允许跳转
return true;
} else {
return false;
}
}

ignore_user_abort(true);
set_time_limit(0);
while (true) {
SiteMonitorBot(‘http://www.xiaokyun.com’);
sleep(900); //每隔15分钟自动运行
}

?>

Get IP with PHP

<?php

$names = file(‘sites.txt’);
foreach ($names as $name) {
$ip = gethostbyname($name);
echo ‘<li>’ . $ip . ‘</li>’;
}

?>

PHP Ping

<?php

function ping($host, $port, $timeout) {
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "down"; }
$tA = microtime(true);
return round((($tA – $tB) * 1000), 0)." ms";
}

echo ping(‘www.google.com’, 80, 10);

?>

php正则过滤

$str=preg_replace("/s+/", " ", $str); //过滤多余回车
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)
$str=preg_replace("/<!–.*?–>/si","",$str); //注释
$str=preg_replace("/<(!.*?)>/si","",$str); //过滤DOCTYPE
$str=preg_replace("/<(/?html.*?)>/si","",$str); //过滤html标签
$str=preg_replace("/<(/?head.*?)>/si","",$str); //过滤head标签
$str=preg_replace("/<(/?meta.*?)>/si","",$str); //过滤meta标签
$str=preg_replace("/<(/?body.*?)>/si","",$str); //过滤body标签
$str=preg_replace("/<(/?link.*?)>/si","",$str); //过滤link标签
$str=preg_replace("/<(/?form.*?)>/si","",$str); //过滤form标签
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签
$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(/?applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(/?style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(/?title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(/?objec.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/<(/?script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签
$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签
$str=preg_replace("/on([a-z]+)s*=/si","On1=",$str); //过滤script标签
$str=preg_replace("/&#/si","&#",$str); //过滤script标签

PHP 删除文本文件的任意一行或添加一行

<?php
$filename="aaa.txt";//定义操作文件
$delline=3; //要删除的行数
$farray=file($filename);//读取文件数据到数组中
for($i=0;$i<count($farray);$i++)
{
if(strcmp($i+1,$delline)==0) //判断删除的行,strcmp是比较两个数大小的函数
{
continue;
}
if(trim($farray[$i])<>"") //删除文件中的所有空行
{
$newfp.=$farray[$i]; //重新整理后的数据
}
}
$fp=@fopen($filename,"w");//以写的方式打开文件
@fputs($fp,$newfp);
@fclose($fp);
?>

添加一行数据到最后一行

<?php
$filename="aaa.txt";//定义操作文件
$fcontent = file($filename); //file()把整个文件读入一个数组中
$fp = fopen("$filename","a");
$str = "10.8.0.9:255.255.255.0n";
fwrite($fp, $str);
?>