PHP及相關服務器防盜鏈

服務器防盜鏈php

假設域名爲www.localhost.comnginx

  1.apache配置httpd.conf    apache

 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設置  spa

<?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");
  
?>
相關文章
相關標籤/搜索