1. 程式人生 > >PHP及相關服務器防盜鏈

PHP及相關服務器防盜鏈

cas 禁止訪問 expires 提示 防盜鏈 match jpeg 域名 str

服務器防盜鏈

假設域名為www.localhost.com

  1.apache配置httpd.conf    

 SetEnvIfNoCase Referer "^http://www.localhost.com/" local_ref=1 
<FilesMatch ".(gif|jpg)"> 
Order Allow,Deny 
Allow from env=local_ref 
</FilesMatch>  

  2.apache配置.htaccess  

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !^http://(www.)?localhost.com /.*$ [NC] 
RewriteRule .(gif|jpg)$ http://www.localhost.com/noAllowed.gif [R,L]  #####設置非指定域名下的防盜鏈圖片展示

  3.nginx設置/usr/local/nginx/conf/nginx.conf這個文件,將如下代碼匹配在server{ }段裏面即可

  

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires 30d;
    valid_referers www.locahost.com;
if ($invalid_referer) {
return 403;#禁止訪問
     #rewrite ^(.*)$ 403.jpg ###設置防盜鏈提示圖片
} }

  45.php設置  

<?php
//獲取referer 瀏覽器打開的頁面沒有referer
  if(isset($_SERVER[‘HTTP_REFERER‘]))
  {
      if(strpos($_SERVER[‘HTTP_REFERER‘],"http://www.localhost.com/")==0)
      {
       ...
      } else { 
     header("Location:403.php");//跳轉頁面到warning.php
    } } else header("Location:403.php"); ?>

  

PHP及相關服務器防盜鏈