2012-10-02 5 views
2

검색 엔진 트래픽에만 Google 애드 센스 광고를 게재하고 직접 또는 Facebook, Twitter, 이메일 링크 등에서 오는 일반 방문자에게는 광고를 표시하지 않습니다.Google 애드 센스 광고를 검색 엔진 트래픽에만 표시하는 방법은 무엇입니까?

여기에 현재 사용하고있는 코드가 있습니다. 잘 작동하지만 Bing, Yahoo, Ask 등과 같은 Google 이외에 많은 다른 검색 엔진을 포함하도록 코드를 개선하고 싶습니다. 누군가 아래의 코드를보고 개선점을 수정해도 되겠습니까?

<?php 
$ref = $_SERVER['HTTP_REFERER']; 
if (preg_match("(google|yahoo|bing)", $ref) != false) { 
echo <<<END 
<script type="text/javascript"><!-- 
google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx"; 
/* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */ 
google_ad_slot = "xxxxxxxxxxxxxx"; 
google_ad_width = xxx; 
google_ad_height = xxx; 
//--> 
</script> 
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
END; 
} 
else { 
    echo "" 
; 
} 
?> 

답변

3

코드가 상당히 양호합니다. 제안 사항은 패턴 구분 기호로 ( 대신 /을 사용하는 것이고 괄호는 일치 그룹에도 사용할 수 있습니다. 대/소문자를 구분하지 않으려면 i 플래그를 추가 할 수도 있습니다. else 문은 출력하고 빈 문자열 만 필요하기 때문에 필요하지 않습니다. END HEREDOC에도 </div> 태그가 추가로 있습니다. 열기 및 닫기가 모두 if 문 내부 또는 외부에 있는지 확인하고자합니다. 다음에

<?php 
$referrer = $_SERVER['HTTP_REFERER']; 
$my_domain = "example.com"; 
$search_engines = "google|yahoo|bing|altavista|digg"; 
$pattern = "((http(s)?:\/\/)(\w+?\.)?(?!{$my_domain})({$search_engines}))"; 
if (preg_match("/{$pattern}/i", $referrer) != false) { 
    echo <<<END 
    <script type="text/javascript"><!-- 
    google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx"; 
    /* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */ 
    google_ad_slot = "xxxxxxxxxxxxxx"; 
    google_ad_width = xxx; 
    google_ad_height = xxx; 
    //--> 
    </script> 
    <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
    </script> 
END; 
} else { 
    // Show something to visitors not referred by a search engine 
} 
?> 

정규식 패턴 결과는 * 표시가 일치 :

FALSE - http://example.com/google 
FALSE - http://example.com/google.com 
FALSE - http://www.example.com/google.com 
TRUE - *http://google*.com/example.com 
TRUE - *http://www1.google*.com/example.com 
TRUE - *http://www.google*.com/example.com 
TRUE - *http://images.google*.com/page 
TRUE - *https://google*.com/example.com 
TRUE - *https://www.google*.com/example.com 
+0

을 감사 와우! 꽤 멋지다. 내가 "상당히 좋다"라고 코딩 한 것을 듣는다. 정말 멋지다. 그리고 코드를 수정하고 나에게 그렇게 훌륭한 설명을 해주셔서 감사드립니다. 나는 그 점을 정말로 고맙게 생각한다. if 문은 일반 시청자에게 다른 것을 보여줍니다. 나는 그것을 다시 추가 할 수 있다고 생각한다. 다시 한번 doubelyarp에게 감사한다. – Garry

+0

이봐 @doublesharp이 오류가 점점 오전 : 나는이 오류 메시지가 점점 오전 : '구문 분석 오류 : 구문 오류, 예기치 못한 $ 끝, 기대를 T_VARIABLE 또는 /var/www/xxxxx.com/public_html에서 T_END_HEREDOC 또는 T_DOLLAR_OPEN_CURLY_BRACES 또는 T_CURLY_OPEN /xxx-xxx-xx.php on line 18' – Garry

+0

'END'는 들여 쓰기 할 수 없습니다. 여기서 해결책을 찾았습니다. http://www.codingforums.com/showpost.php?s=f1da607bd751dc4213191c3d82624a4d&p=1253001&postcount=2 – Garry

관련 문제