How to protect against direct access to images?

前端 未结 9 1049
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 00:47

I would like to create a web site with many images. But I would like to protect against direct access to images, e.g. direct links to images without visiting the web site.

9条回答
  •  暖寄归人
    2020-12-05 01:15

    I use both methods - checking user gent and and referrer. User agent I check at .htaccess. And referrer check at php file. You can see it at http://coloring-4kids.com

    Here is my code:

    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?pinterest\.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?pinterest\.com$ [NC]
    
    RewriteCond %{HTTP_USER_AGENT} !(Googlebot|bingbot|msnbot|yahoo-mmcrawler|YandexImages) [NC]
    RewriteCond %{HTTP_USER_AGENT} !googlebot-image [NC]
    RewriteCond %{HTTP_USER_AGENT} !googlebot [NC]
    RewriteCond %{HTTP_USER_AGENT} !googlebot-news [NC]
    RewriteCond %{HTTP_USER_AGENT} !googlebot-video [NC]
    RewriteCond %{HTTP_USER_AGENT} !googlebot-mobile [NC]
    RewriteCond %{HTTP_USER_AGENT} !mediapartners-google [NC]
    RewriteCond %{HTTP_USER_AGENT} !mediapartners [NC]
    # RewriteCond %{HTTP_USER_AGENT} !adsbot-google [NC]
    RewriteCond %{HTTP_USER_AGENT} !bingbot [NC]
    RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit [NC]
    RewriteCond %{HTTP_USER_AGENT} !baiduspider [NC]
    RewriteCond %{HTTP_USER_AGENT} !yandex [NC]
    RewriteCond %{HTTP_USER_AGENT} !sogou [NC]
    RewriteCond %{HTTP_USER_AGENT} !twitterbot [NC]
    RewriteCond %{HTTP_USER_AGENT} !pinterest [NC]
    
    
    RewriteRule (^.*\.(gif)$) /watermark.php?src=$1 [L]
    

    watermark.php

     400) && ($pinproverka!=true) )  { imagecopymerge($image, $watermark, $dest_x - 5, 5, 0, 0, $watermark_width, $watermark_height, 100); }
    
    
    
     imagegif($image);
    
     imagedestroy($image);
     imagedestroy($watermark);
    
    ?>
    

提交回复
热议问题