RadarURL
Skip to content
2011.09.22 23:14

외부 링크 방지

조회 수 6112 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

개인 계정에서 블로그등의 사이트를 운영하시는 분들 중 가끔 외부에서의 컨텐츠 직링크로 인해 트래픽 초과에 걸리시는 분들이 많습니다. 트래픽 문제가 아니더라도, 무단 링크로 인해 저작권을 침해받는 경우도 있겠죠.
이를 해결하기 위해 레퍼러비교 같은 방법도 쓰곤 하는데, 아마 .htaccess 파일을 이용한 것도 많이들 아실 겁니다.

간단히, .htaccess와 워터마킹 기술을 이용해 이미지 링크 제어를 하는 법을 소개합니다.

일단, 이미지들이 저장되는 디렉토리에 .htaccess파일을 만듭니다. (이미 존재한다면 아래의 소스를 마지막에 추가하시면 됩니다.)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !explug.com [NC]
RewriteCond %{HTTP_REFERER} !eouia0. [NC]
RewriteCond %{HTTP_REFERER} !google. [NC]
RewriteCond %{HTTP_REFERER} !search?q=cache [NC]
RewriteRule (.*) image.php?image=$1

...........
잠깐 설명을 하자면,

RewriteCond %{HTTP_REFERER} !explug.com [NC]
RewriteCond %{HTTP_REFERER} !eouia0. [NC]
RewriteCond %{HTTP_REFERER} !google. [NC]
RewriteCond %{HTTP_REFERER} !search?q=cache [NC]

이 부분에 링크를 허용할 URL들을 기재합니다. 정규식 패턴이므로 잘 알아서.. :)
위의 예의 경우 explug.com, eouia0가 포함되는 사이트, google 등에 링크를 허용한 경우입니다. 이 경우에는 원본 이미지를 마음대로 가져다 쓸 수 있습니다.
그외의 URL에서 링크가 걸릴 경우에는

RewriteRule (.*) image.php?image=$1

에 따라, image.php?image=파일이름 으로 리다이렉트됩니다. 만약 그냥 링크 자체를 끊고 싶으시다면 여기에 아무거나 써주셔도 되지요.

이제 워터마킹 처리를 위해 image.php를 작성합니다. GD라이브러리를 사용했으므로 GD가 사용가능한 계정이어야 합니다.

<?
define ("WATERMARK", "watermark.jpg");

header("Content-type: image/jpeg");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$pic = strip_tags( $_GET['image'] );

$image_url = urldecode($pic);

if (!@fopen($image_url, "r")) {
   $dst_img = imagecreatefromjpeg(WATERMARK);
   $res = imagejpeg($dst_img, "", 100);
   die();
}

$file = pathinfo($image_url);
switch(strtoupper($file["extension"])) {
   case "JPG":
       $src_img = imagecreatefromjpeg($image_url);
       break;
   case "GIF":
       $src_img = imagecreatefromgif($image_url);
       break;
   case "PNG":
       $src_img = imagecreatefrompng($image_url);
       break;
}

$src_w = imagesx($src_img);
$src_h = imagesy($src_img);

$portion = $src_h / $src_w;
$dest_w = 300;
$dest_h = round($dest_w * $portion);

$dst_img = imagecreatetruecolor($dest_w, $dest_h);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_w, $dest_h, $src_w, $src_h);

$watermark = imagecreatefromjpeg(WATERMARK);

$watermark_w = imagesx($watermark);
$watermark_h = imagesy($watermark);

$overlay_img = imagecreatetruecolor($watermark_w, $watermark_h);
imagecopy($overlay_img, $watermark, 0,0,0,0, $watermark_w, $watermark_h);
imagedestroy($watermark);

$white  = imagecolorallocate($overlay_img, 0xFF, 0xFF, 0xFF);
imagecolortransparent($overlay_img, $white);

$offsetX = $dest_w - $watermark_w - 3;
$offsetY = $dest_h - $watermark_h - 3;

imagecopymerge($dst_img,$overlay_img,$offsetX,$offsetY,0,0,$watermark_w,$watermark_h, 100);
imagedestroy($overlay_img);

$res = imagejpeg($dst_img, "", 100);
imagedestroy($dst_img);
?>

<VirtualHost *:80>
    DocumentRoot ...
    ServerName sir.co.kr
    ServerAlias www.sir.co.kr wwww.sir.co.kr ww.sir.co.kr
    #  외부링크방지
    SetEnvIfNoCase Referer sir.co.kr link_allow
    SetEnvIfNoCase Referer www.sir.co.kr link_allow
    SetEnvIfNoCase Referer ^$ link_allow
    <FilesMatch
".(jpe?g|gif|png|bmp|avi|swf|mpe?g|zip|z[00-99]|rar|mp[1-9]|arj|exe|asf|wm[a-z]*|ra[a-z]*|alz|ZIP|Z[00-99])$">
        Order Deny,Allow
        Allow from env=link_allow
        Deny from all
    </FilesMatch>
</VirtualHost>

 

출처 : http://blog.ohmynews.com/hjlee222/185568

?

  1. Apache에서 .htaccess 파일 활용하기 - Rewrite mod

    Date2009.05.12 ByJaeSoo Views3819
    Read More
  2. 계정하나에 두개의 도메인을 각각의 서브폴더에 연결시키는 방법

    Date2009.06.23 ByJaeSoo Views4848
    Read More
  3. ReWrite Module

    Date2009.06.24 ByJaeSoo Views3948
    Read More
  4. .htaccess

    Date2009.06.24 ByJaeSoo Views4322
    Read More
  5. Tomcat 한글관련

    Date2009.07.20 ByJaeSoo Views4489
    Read More
  6. 웹서버 속도 저하 문제 해결 (xp,Apache 2.2.X, XAMPP)

    Date2010.04.05 ByJaeSoo Views5837
    Read More
  7. 가상 호스트 설정법

    Date2010.04.28 ByJaeSoo Views6950
    Read More
  8. 싸이트 외부링크 차단하는방법

    Date2011.02.19 ByJaeSoo Views7187
    Read More
  9. APMSETUP 7 + eAccelerator 설치..

    Date2011.05.21 ByJaeSoo Views9518
    Read More
  10. 아파치 프로세스의 메모리 사용량 줄이기

    Date2011.06.01 ByJaeSoo Views9324
    Read More
  11. 불법웹(이미지) 링크 차단에 대한 설정법

    Date2011.09.22 ByJaeSoo Views7315
    Read More
  12. 외부 링크 방지

    Date2011.09.22 ByJaeSoo Views6112
    Read More
  13. WAMP (Windows+Apache+MySQL+PHP) 설치

    Date2011.11.14 ByJaeSoo Views5417
    Read More
  14. 아파치 리라이트(rewrite) 엔진과 워터마킹을 이용한 이미지 무단 링크 방지

    Date2012.03.07 ByJaeSoo Views2936
    Read More
  15. .htaccess 사용해서 이미지 외부링크 막기

    Date2012.03.07 ByJaeSoo Views2919
    Read More
  16. 동영상 외부 링크 금지

    Date2012.03.07 ByJaeSoo Views3106
    Read More
  17. .htaccess 를 이용한 이미지 핫링크 예방법

    Date2012.03.07 ByJaeSoo Views2630
    Read More
  18. APMSETUP + JSP

    Date2012.04.13 ByJaeSoo Views6125
    Read More
  19. Windows용 아파치 설정하기 (쓰레드 설정)

    Date2012.07.12 ByJaeSoo Views1955
    Read More
  20. Comparison of WAMPs (Windows, Apache, MySQL, PHP/Perl/Python)

    Date2012.07.13 ByJaeSoo Views4744
    Read More
Board Pagination Prev 1 2 3 4 Next
/ 4

PageViews   Today : 11619 Yesterday : 5037 Total : 21967671  /  Counter Status   Today : 11247 Yesterday : 4602 Total : 1193970

Edited by JAESOO

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소