考虑到友友的不方便,还是放出了文字版的吧
auto_job.php文件
<?php ########这里设置数据库信息
static $_config=array();
$_config['data_ip']="localhost"; //数据库的IP
$_config['data_name']="jao"; //数据库名字
$_config['data_user_name']="jao"; //数据库的用户名
$_config['data_user_pass']="jao123456"; //数据库的密码
$_config['table_dbf']="jao_"; //表的前缀
class data_class{
private $data_name;
private $user_name;
private $user_pass;
private $data_address;
private $conn;
//private $sql_num=0;
private $sql="";
private function __get($property_name){
if(isset($this->$property_name)){
return ($this->$property_name);
}else{
return (NULL);
}
}
private function __set($property_name,$value){
$this->$property_name=$value;
}
function __construct($ip,$data,$user,$pass){
$this->data_address=$ip;
$this->data_name=$data;
$this->user_name=$user;
$this->user_pass=$pass;
@$this->conn=mysql_connect($this->data_address,$this->user_name,$this->user_pass) or die("
\n
\n
\r\n
\n
\r\n
\r\n
\r\n
\r\n
\r\n
\r\n
\r\n
建设中 \r\n
\r\n
\r\n
网站建设中...
\r\n
报时:".date('m-d H:s')."
");
self::query("set names 'utf8'");
$this->select=mysql_select_db("$this->data_name",$this->conn);
}
public function query($sql){
//$sql_num++;
//echo $sql."
";
return mysql_query($sql,$this->conn);
}
function __destruct(){
if($this->conn){
mysql_close($this->conn);
}
}
}
#######
class UrlPost{
static private function UrlFsockPost($url,$vars){
//$vars=a==b&c=d且urlencode
$url=parse_url($url);
$host=$url['host'];
$uri=isset($url['query'])?$url['query']:'';
$PathAndName='/'.$url['path'].'?'.$uri;
$fp = fsockopen($host,80);
if (!$fp) {
die("连接失败
\n");
} else {
$out = "POST {$PathAndName} HTTP/1.1\r\n";
$out .= "Host: {$host}\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-length: ".strlen($vars)."\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= "{$vars}";
fwrite($fp, $out);
$str='';
while (!feof($fp)) {
$str.=fgets($fp,4096);
}
return $str;
fclose($fp);
}
}
static private function UrlFilePost($url, $post ) {
$context = array();
if (is_array($post)){
$context['http'] = array(
'method' => 'POST',
'content' => http_build_query($post),
);
}
if($text=file_get_contents($url, false, stream_context_create($context))){
return $text;
}else{
return 'false';;
}
}
static private function UrlCurlPost ($url,$vars){
$ch = curl_init();
//curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_URL, $url);
//curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
if($str=curl_exec ($ch)){
return $str;
}else{
return 'false';
}
curl_close($ch);
}
static public function Post($url,$post){
if(function_exists('file_get_contents')){
$str=self::UrlFilePost($url,$post);
}elseif(function_exists('fsockopen')){
$vars=implode('&',$post);
$str=self::UrlFsockPost($url,$vars);
}elseif(function_exists('curl_init')){
$vars=implode('&',$post);
$str=self::UrlCurlPost($url,$vars);
}else{
die("你的空间不支持file_get_contents和curl_init和fsockopen所以不能使用啦!");
}
return $str;
}
}
?>
query($sql)){
$my->query("INSERT INTO `{$_config['table_dbf']}setjob` (`id`, `open`, `load_time`, `jiange`, `num`, `url`, `type`, `post_str`, `group`) VALUES
(1, 1, 1353645751, 1, 2212, NULL, 'get', NULL, '默认分组')");
unset($sql);
}
$sql="select * from `{$_config['table_dbf']}setjob` where `open`=1 and `load_time`<".time()." and `num`>0 and `id`<>1 order by `load_time` limit 5";
$rs=$my->query($sql);
$i=0;
if($row=mysql_fetch_array($rs)){
do{
$id=$row['id'];
$url=$row['url'];
$type=$row['type'];
$post_str=$row['post_str'];
$group=$row['group'];
$num=$row['num']-1;
$jiange=$row['jiange'];
$now_time=time();
$load_time=$now_time+$jiange;
$sql2="update `{$_config['table_dbf']}setjob` set `load_time`='{$load_time}',`num`=`num`-1 where `id`='{$id}'";
$my->query($sql2);
$url=str_replace('&','@@',$url);
$url="url={$url}&type={$type}&post_str={$post_str}";
goto_url($url);
usleep(100000); //延迟0.1秒
$i++;
}while($row=mysql_fetch_array($rs));
}else{
echo '没有需要执行的任务,已退出此次请求
';
$end_time=microtime();
$end_time=explode(" ",$end_time);
$end_time=$end_time[0];
echo '耗时:'.round($end_time-$start_time,2).'
';
ob_end_flush();
sleep(1);
load_self();
exit;
}
mysql_free_result($rs); unset($sql);
echo '此次执行任务'.$i.'个
';
$end_time=microtime();
$end_time=explode(" ",$end_time);
$end_time=$end_time[0];
echo '耗时:'.round($end_time-$start_time,2).'
';
ob_end_flush();
sleep(1);
load_self();
function load_self(){
global $my,$_config;
$sql="select `load_time` from `{$_config['table_dbf']}setjob` where `id`=1";
$rs=$my->query($sql);
$row=mysql_fetch_array($rs);
$now_time=time();
if ($row['load_time']<$now_time){
$my->query("update `{$_config['table_dbf']}setjob` set `load_time`='{$now_time}',`num`=`num`+1 where `id`=1");
$srv_ip = $_SERVER['HTTP_HOST']; //你的目标服务地址.
$srv_port = 80; //端口
$url = $_SERVER['HTTP_HOST'];
$errno = 0; //错误处理
$errstr = '' ; //错误处理
$timeout = 10; //多久没有连上就中断
file_put_contents("setjob_log.txt",ob_get_contents().date("m-d H:i:s"));
$fp = fsockopen ( $srv_ip , $srv_port , $errno , $errstr , $timeout );
if (! $fp ){
echo ( 'fp fail' );
}
$post_header = "GET {$_SERVER['REQUEST_URI']} HTTP/1.1\r\n" ;
$post_header .= "Host: ".$url.":".$srv_port ;
$post_header .= "Connection: close\r\n\r\n" ;
fwrite( $fp , $post_header );
fclose( $fp );
/* eval(base64_decode(""));*/
return true;
} mysql_free_result($rs);
}
function goto_url($_url='http://m.bolatu.org'){
$srv_ip = $_SERVER['HTTP_HOST']; //你的目标服务地址.
$srv_port = 80; //端口
$url = $_SERVER['HTTP_HOST'];
$errno = 0; //错误处理
$errstr = '' ; //错误处理
$timeout = 10; //多久没有连上就中断
file_put_contents("setjob_log_2.txt",ob_get_contents().date("m-d H:i:s"));
$fp = fsockopen ( $srv_ip , $srv_port , $errno , $errstr , $timeout );
if (! $fp ){
die ( 'fp fail' );
}
$name=$_SERVER['SCRIPT_NAME'];
$name=str_replace('job','job_2',$name);
$post_header = "GET {$name}?".$_url." HTTP/1.1\r\n" ;
$post_header .= "Host: ".$url.":".$srv_port ;
$post_header .= "Connection: close\r\n\r\n" ;
fwrite( $fp , $post_header );
fclose( $fp );
/* eval(base64_decode(""));*/
return true;
}
?>
第二个文件:auto_job_2.php
<?php
class UrlPost{
static private function UrlFsockPost($url,$vars){
//$vars=a==b&c=d且urlencode
$url=parse_url($url);
$host=$url['host'];
$uri=isset($url['query'])?$url['query']:'';
$PathAndName='/'.$url['path'].'?'.$uri;
$fp = fsockopen($host,80);
if (!$fp) {
die("连接失败
\n");
} else {
$out = "POST {$PathAndName} HTTP/1.1\r\n";
$out .= "Host: {$host}\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-length: ".strlen($vars)."\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= "{$vars}";
fwrite($fp, $out);
$str='';
while (!feof($fp)) {
$str.=fgets($fp,1024);
}
return $str;
fclose($fp);
}
}
static private function UrlFilePost($url, $post ) {
$context = array();
if (is_array($post)){
$context['http'] = array(
'method' => 'POST',
'content' => http_build_query($post),
);
}
if($text=file_get_contents($url, false, stream_context_create($context))){
return $text;
}else{
return 'false';;
}
}
static private function UrlCurlPost ($url,$vars){
$ch = curl_init();
//curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_URL, $url);
//curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
if($str=curl_exec ($ch)){
return $str;
}else{
return 'false';
}
curl_close($ch);
}
static public function Post($url,$post){
if(function_exists('file_get_contents')){
$str=self::UrlFilePost($url,$post);
}elseif(function_exists('fsockopen')){
$vars=implode('&',$post);
$str=self::UrlFsockPost($url,$vars);
}elseif(function_exists('curl_init')){
$vars=implode('&',$post);
$str=self::UrlCurlPost($url,$vars);
}else{
die("你的空间不支持file_get_contents和curl_init和fsockopen所以不能使用啦!");
}
return $str;
}
}
header("Content-Type: text/html; charset=utf-8");
ignore_user_abort(true);
date_default_timezone_set('PRC');
set_time_limit (30);
$url=urldecode($_GET['url']);
$url=str_replace('@@','&',$url);
$type=$_GET['type'];
$post_str=$_GET['post_str'];
file_put_contents("index.txt","执行:".date("m-d H:i:s"));
if($type=='post'){
UrlPost::UrlFsockPost($url,$post_str);
}else{
file_get_contents($url);
}
?>