PHP本地执行放在远程服务器上的脚本
Submitted by on 2006, November 9, 10:38 PM. 技术
<?php
//file文件中存放的是要执行的php脚本
$file="http://www.abc.com/abc.php";
if (!remote_file_exists($file))
{
error_info();
} else {
$string=http_get($file);
eval($string);
}
function error_info()
{
$str= $_SERVER['REQUEST_URI'];
if (strstr($str,"?"))
{
$ser= explode("/",$_SERVER['REQUEST_URI']);
$errfile=explode("?", $ser[2]);
$errorfile=$errfile[0];
}
else
{
$errfile= explode("/",$_SERVER['REQUEST_URI']);
$errorfile=$errfile[2];
}
$errorinfo="
<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /$errorfile
was not found on this server.</p>
</body></html>";
echo $errorinfo;
exit;
}
function http_get($url)
{
$url_stuff = parse_url($url);
$port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;
$fp = fsockopen($url_stuff['host'], $port);
$query = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
$query .= 'Host: ' . $url_stuff['host'];
$query .= "\n\n";
fwrite($fp, $query);
while ($tmp = fread($fp, 1024))
{
$buffer .= $tmp;
}
preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
return substr($buffer, - $parts[1]);
}
function remote_file_exists($url_file){
//检测输入
$url_file = trim($url_file);
if (empty($url_file)) { return false; }
$url_arr = parse_url($url_file);
if (!is_array($url_arr) || empty($url_arr)){ return false; }
//获取请求数据
$host = $url_arr['host'];
$path = $url_arr['path'] ."?". $url_arr['query'];
$port = isset($url_arr['port']) ? $url_arr['port'] : "80";
//连接服务器
$fp = fsockopen($host, $port, $err_no, $err_str, 30);
if (!$fp){ return false; }
//构造请求协议
$request_str = "GET ".$path." HTTP/1.1\r\n";
$request_str .= "Host: ".$host."\r\n";
$request_str .= "Connection: Close\r\n\r\n";
//发送请求
fwrite($fp, $request_str);
$first_header = fgets($fp, 1024);
fclose($fp);
//判断文件是否存在
if (trim($first_header) == ""){ return false; }
if (!preg_match("/200/", $first_header)){
return false;
}
return true;
}
?>