PHP 自动挂链函数。
此函数可直接用于批量挂链接,方便站群用户管理多个友情链接。
使用此代码需设置远程链接输出服务端。远程服务端不再本文内容所述中。
注意:此代码仅供参考,如应用到实际中需要少量更改。
此代码中部分标记需做处理,否则容易被搜索引擎识别为友情链接出售,从而受到打击。
使用本代码出现任何问题可联系本博主解决,联系方式在本站下方也可直接邮件 [email protected]
本代码参考多家友情链接平台自动挂链技术精简出来的。
附本代码如下:
<?php
function cacheExecute(){
//在此设置网站静态首页的文件名,默认为:"index.html"
$IndexFileName = dirname(__FILE__) . "index.html";
$TempFileName = "";
$TempName = dirname(__FILE__) . "/" . $TempFileName;
//在此设置网站静态首页的编码方式,默认为:"gb2312",请正确设置不然会导致乱码
$IndexFileEncoding = "UTF-8";
if(!file_exists($IndexFileName)){
echo "no_index_file";
}
else{
$out = cache_output();
if(@out!=""){
$html = file_get_contents($IndexFileName);
$tmp = split('<!--cache code start', $html);
$tmp = split('<!--cache code end-->', $tmp[1]);
$str = "<!--cache code start$tmp[0]<!--cache code end-->";
if($str != $out){
$html = str_replace($str, $out, $html);
cache_file_put_contents($IndexFileName, $html);
}
echo "cache";
if($TempFileName!=""){
if(file_exists($TempName)){
$html = file_get_contents($TempName);
$tmp = split('<!--cache code start', $html);
$tmp = split('<!--cache code end-->', $tmp[1]);
$str = "<!--cache code start$tmp[0]<!--cache code end-->";
if($str != $out){
$html = str_replace($str, $out, $html);
cache_file_put_contents($TempName, $html);
}
}
}
}
else{
echo "no_data";
}
}
}
function cache_file_put_contents($filename, $data){
if (!function_exists('file_put_contents')) {
$f = @fopen($filename, 'w');
if (!$f) {
return false;
} else {
$bytes = fwrite($f, $data);
fclose($f);
return $bytes;
}
}
else{
return file_put_contents($filename, $data);
}
}
function cache_ad_helper($url)
{
$content = '';
$done=false;
if (ini_get('allow_url_fopen') == '1') {
if ($fp = @fopen($url, 'r')) {
while ($line = @fread($fp, 1024)) {
$content .= $line;
$done=true;
}
}
}
if (!$done) {
// get the host name and url path
$parsedUrl = parse_url($url);
$host = $parsedUrl['host'];
if (isset($parsedUrl['path'])) {
$path = $parsedUrl['path'];
}
$timeout = 10;
// connect to the remote server
$fp = @fsockopen($host, '80', $errno, $errstr, $timeout );
if( !$fp ) {
// echo "Cannot retrieve $url";
} else {
// send the necessary headers to get the file
@fputs($fp, "GET $path HTTP/1.0\r\n" .
"Host: $host\r\n\r\n");
// retrieve the response from the remote server
while ( $line = @fread( $fp, 4096 ) ) {
$content .= $line;
}
@fclose( $fp );
// strip the headers
$pos = strpos($content, "\r\n\r\n");
$content = substr($content, $pos + 4);
}
}
$content=str_replace("Bad Request (Invalid Hostname)","",$content);
$content=str_replace("Service Unavailable","",$content);
if (!preg_match('/\<Error/',$content)) {
return $content;
}
// return the file content
return $content;
}
function cache_output(){
$url = "http://www.23to.com";
return urldecode(cache_ad_helper($url));//如果出现乱码,请换成这个:return iconv("UTF-8",$IndexFileEncoding,urldecode(cache_ad_helper($url)));
}
cacheExecute();
?>
雨田博客