📖
핫링크 방지된 외부 이미지를 표시하려면

페이지 정보

본문

PHP 프록시 서버를 통해 중계하는 방식이 가장 안정적이고 적절합니다. (img_proxy.php 예제)
[code]<?php
// 이미지 URL을 쿼리에서 직접 받음
$imageUrl = $_GET['url'] ?? '';

if (!$imageUrl || !filter_var($imageUrl, FILTER_VALIDATE_URL)) {
    // 유효하지 않으면 noimg.jpg 출력
    header("Content-Type: image/jpeg");
    readfile(__DIR__ . "/home/img/noimg.jpg");
    exit;
}

// 원격 이미지 가져오기
$ch = curl_init($imageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
$imageData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);

if ($httpCode !== 200 || !$imageData) {
    header("Content-Type: image/jpeg");
    readfile(__DIR__ . "/home/img/noimg.jpg");
    exit;
}

// 성공 시 원격 이미지 출력
header("Content-Type: {$contentType}");
echo $imageData;
exit;
[/code]
사용법[code]<img src="img_proxy.php?url=이미지url" />[/code]url 파라미터에 실제 원격 이미지 URL을 넣으면 됩니다.
유효하지 않거나 실패 시 /home/img/noimg.jpg 이미지를 대신 보여줍니다.

특정 도메인만 허용하는 필터를 넣고 싶으면,
아래처럼 $allowed_domains 배열을 만들고, parse_url()로 도메인 추출 후 체크하면 됩니다.[code]<?php
$imageUrl = $_GET['url'] ?? '';

if (!$imageUrl || !filter_var($imageUrl, FILTER_VALIDATE_URL)) {
    header("Content-Type: image/jpeg");
    readfile(__DIR__ . "/home/img/noimg.jpg");
    exit;
}

// 허용 도메인 리스트 (예: musictrot.com, xpx.kr)
$allowed_domains = ['musictrot.com', 'xpx.kr'];

// URL에서 호스트 추출
$host = parse_url($imageUrl, PHP_URL_HOST);
if (!$host) {
    header("Content-Type: image/jpeg");
    readfile(__DIR__ . "/home/img/noimg.jpg");
    exit;
}

// 허용 도메인 검사 (서브도메인 포함)
$allowed = false;
foreach ($allowed_domains as $domain) {
    if (stripos($host, $domain) !== false) {
        $allowed = true;
        break;
    }
}
if (!$allowed) {
    header("Content-Type: image/jpeg");
    readfile(__DIR__ . "/home/img/noimg.jpg");
    exit;
}

// 원격 이미지 가져오기 (이하 동일)
$ch = curl_init($imageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
$imageData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);

if ($httpCode !== 200 || !$imageData) {
    header("Content-Type: image/jpeg");
    readfile(__DIR__ . "/home/img/noimg.jpg");
    exit;
}

header("Content-Type: {$contentType}");
echo $imageData;
exit;
[/code]$allowed_domains 에 원하는 도메인을 넣어 관리하세요.
서브도메인까지 허용하려면 stripos 체크 방식을 사용합니다.
도메인 불일치 시 noimg.jpg 출력으로 차단됩니다.

그누보드 적용예시[code]<?php
include_once('../common.php');

$bo_table = $_GET['bo_table'] ?? 'free';
$wr_id = intval($_GET['wr_id'] ?? 0);

// DB에서 썸네일 URL 가져오기
$sql = "SELECT wr_10 FROM g5_write_{$bo_table} WHERE wr_id = {$wr_id}";
$row = sql_fetch($sql);
$thumbnailUrl = $row['wr_10'] ?? '';

if ($bo_table == "video" && $thumbnailUrl) {
    $thumbnailUrl = str_replace("_tiny", "_large", $thumbnailUrl);
}

if ($thumbnailUrl && filter_var($thumbnailUrl, FILTER_VALIDATE_URL)) {
    // cURL로 이미지 가져오기
    $ch = curl_init($thumbnailUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
    //curl_setopt($ch, CURLOPT_REFERER, 'https://blog.kakao.com'); // 필요시 추가
    $imageData = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    curl_close($ch);

    if ($httpCode !== 200 || !$imageData) {
    header("Content-Type: image/jpeg");
    readfile($_SERVER['DOCUMENT_ROOT'] . "/home/img/noimg.jpg");
    }

    // Content-Type 자동 출력
    header("Content-Type: {$contentType}");
    echo $imageData;
    exit;
} else {
    header("Content-Type: image/jpeg");
    readfile($_SERVER['DOCUMENT_ROOT'] . "/home/img/noimg.jpg");
    exit;
}[/code]
사용 예시[code]<img src="proxy_image.php?bo_table=video&wr_id=123">[/code]

댓글목록

등록된 댓글이 없습니다.


자료 목록
번호 제목 날짜
📖
🫧
06-02
199
🫧
05-23
198
🫧
05-23
197
🫧
05-23
196
🫧
05-23
195
🫧
05-22
194
🫧
05-22
193
🫧
05-22
192
🫧
05-22
191
🫧
05-21
190
🫧
05-16
189
🫧
05-15
188
🫧
05-14
187
🫧
05-11
186
🫧
05-10

🔍 검색

회사소개 개인정보처리방침 서비스이용약관
Copyright © rainbowgarden.shop All rights reserved.