Php 有些图片没有';t加载(CSS问题?)

Php 有些图片没有';t加载(CSS问题?),php,load,dynamic,file-get-contents,image,Php,Load,Dynamic,File Get Contents,Image,我用下面的脚本通过file_get_内容动态加载一个网站 <?php header('Content-Type: text/html; charset=iso-8859-1'); $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}"; $base_url = explode('/', $url); $base_url = (substr($url, 0, 7)

我用下面的脚本通过file_get_内容动态加载一个网站

<?php 
header('Content-Type: text/html; charset=iso-8859-1');

$url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
$base_url = explode('/', $url);
$base_url = (substr($url, 0, 7) == 'http://') ? $base_url[2] : $base_url[0];

if (file_get_contents($url) != false) {

$content = @file_get_contents($url);

// $search = array('@(<a\s*[^>]*href=[\'"]?(?![\'"]?http))@', '|(<img\s*[^>]*src=[\'"]?)|');
// $replace = array('\1proxy2.php?url=', '\1'.$url.'/');
// $new_content = preg_replace($search, $replace, $content);


function prepend_proxy($matches) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend = $matches[2] ? $matches[2] : $url;
    $prepend = 'http://h899310.devhost.se/proxy/proxy2.php?url='. $prepend .'/';

    return $matches[1] . $prepend . $matches[3];
}

function imgprepend_proxy($matches2) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend2 = $matches2[2] ? $matches2[2] : $url;
    $prepend2 = $prepend2 .'/';

    return $matches2[1] . $prepend2 . $matches2[3];
}


$new_content = preg_replace_callback(
    '|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
    'prepend_proxy',
    preg_replace_callback(
        '|(src=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
        'imgprepend_proxy',
        $content
    )
);

echo "<base href='http://{$base_url}' />";
echo $new_content;


} else {

  echo "Sidan kan inte visas";

}
?>  

您的URL替换方法之一似乎添加了太多的斜杠。访问您的代理提供的其中一个页面,您将看到以以下开头的几个URL:

http:///www.msdn.com
以加载msdn.com为例;CSS不会加载,因为当查看代理页面的源代码时,我们看到CSS的URL是(注意树的前斜杠):

直接查看URL会在脚本中显示一条警告,显示
文件\u get\u内容
无法加载URL:

Warning: file_get_contents(http:///i3.msdn.microsoft.com/global/global-bn20090721.css) [function.file-get-contents]: failed to open stream: No error in D:\users\u190790\h899310.devhost.se\Wwwroot\proxy\proxy2.php on line 9
Sidan kan inte visas
简单看一下您的代码,问题似乎出在
$prepend
;它应该是这样的:

<?php
$prepend = $matches2[2] ? $matches2[2] : $url . '/';
$prepend = $prepend;
?>


这会将代理设置为仅显示文本;css和图像无法通过您的代理加载(或者至少无法正确显示)。

是的,现在它显示站点的图片。但是现在页面上的链接不像以前那样经过脚本:proxy2.php?url=asdsdasdOkey,所以我有一个问题。如何检查它是否是一个.CSS文件,然后只在文件前面加上$url,就像使用IMG prepend函数一样,但是使用CSS。我想那会解决的。但我不知道怎么做。
<?php
$prepend = $matches2[2] ? $matches2[2] : $url . '/';
$prepend = $prepend;
?>
header('Content-Type: text/html; charset=iso-8859-1');