代码之家  ›  专栏  ›  技术社区  ›  psychotik

更改所有img链接以指向PHP中无烹饪域的理想方法

  •  0
  • psychotik  · 技术社区  · 15 年前

    我有一个使用MVC的PHP站点,它动态地为大多数请求构建HTML。 我正在更新我的站点,以便在无需烹饪的域上承载图像/静态内容。 目前,这些图片/css是作为指向相关URL的链接编写的。

    我现在能想到的最好办法是更改所有写出的HTML <img> 标签和CSS链接使用一个php函数,该函数将绝对URL插入无cookie域,而不是相对URL。然而,这涉及到对代码的大量更改,可能会遗漏一些标记/链接。

    有没有更好的处理方法的建议?

    3 回复  |  直到 15 年前
        1
  •  0
  •   Peter Ajtai    15 年前

    SED解决方案:

    对所有HTML文件使用SED。您可以使用PHP(或脚本函数)遍历所有文件。 glob 是一个很好的功能。

    <?php
    // this sed assumes you have 
    // <img src="imgs/example.png" />
    // and want to change it to
    // <img src="http://www.noCookies.com/imgs/example.png" />
    
    // Loop through each .html file in current directory and create an .html2
    // If satisfied w results can finalize .html2 files
    // I use .html2 so you don't overwrite your original files
    
    foreach (glob("*.html") as $file)
    {        
        // You have to escape double quotes
        $lookFor = 'src=\"imgs';
        $replaceWith = 'src=\"http://www.noCookies.com/imgs';
    
        // create the sed command:
        // _g is to do a global search
        $shellCommand = "sed s_{$lookFor}_{$replaceWith}_g {$file}";
    
        // create a NEW file and open it
        $fp = fopen("{$file}2", "w");
    
        // preform sed and write it to the new file
        fwrite($fp, shell_exec($shellCommand));
        fclose($fp);
    }
    ?>
    

    编辑-原始答案如下: 另一个选项是mod rewrite,它可能和原来的一样慢或慢。

    如果您运行apache,并且所有的图像都在一个单独的文件夹中(即使它们不是,但这样做更有效),那么您可以使用mod重写…像这样的……?我有点生疏,但你想用相对路径,用绝对路径切换:

    RewriteEngine on
    RewriteRule ^media/images/(.*) http://noCookies.com/media/images/$1 [L]
    

    我想这绝对是一种方式。然后,您可以随着时间的推移缓慢地更改旧的img源。或者只保留旧的图像,并为您添加的任何新图像添加正确的img-src。

    这里是 mod_rewrite documenation 有点不透明…一些 tutorials on preventing image hotlinking 也会有帮助

    我只记得, this is a good mod_rewrite tips and tricks page

        2
  •  1
  •   Misiur    15 年前

    HTML

    <base>
    

    在子页面上?如果您没有HTML的任何静态部分,我不知道

        3
  •  1
  •   ircmaxell    15 年前

    你可以(如果你懒惰)这样做:

    在请求开始时(第一个文件的顶部):

    ob_start('parseImages');
    

    然后声明函数:

    function parseImages($data, $status) {
        static $body = '';
        switch ($status) {
            case PHP_OUTPUT_HANDLER_START:
            case PHP_OUTPUT_HANDLER_CONT:
                $body .= $data;
                return '';
                break;
            case PHP_OUTPUT_HANDLER_END:
                $body .= $data;
                $dom = new DomDocument();
                $dom->loadHtml($body);
                $imgs = $dom->getElementsByTagName('img');
                foreach ($imgs as $img) {
                    $src = (string) $img->getAttribute('src');
                    if (substr($src, 0, 4) != 'http') {
                        //internal link
                        $src = 'http://my.cookieless.com/' . ltrim($src, '/');
                        $img->setAttribute('src', $src);
                    }
                }
                return $dom->saveHtml();
         }
    }
    
    推荐文章