Advertisement
MikFoxi

Защита яндекс метрики от ботов

Dec 22nd, 2022 (edited)
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. <?php
  2. // указать api key, номер метрики вместо 99999999 и сохранить как скрипт с именем: metrika.php
  3. // на сайт подключить кодом: <script defer src="/metrika.php"></script>
  4.  
  5. $apikey = ''; // API Key с сайта ipscore.biz
  6.  
  7. // определение IP адреса (совместим с cloudflare):
  8. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  9. $ip = strip_tags($_SERVER['HTTP_CF_CONNECTING_IP']);
  10. } else {
  11. $ip = isset($_SERVER['REMOTE_ADDR']) ? strip_tags($_SERVER['REMOTE_ADDR']) : die('// IP');
  12. }
  13.  
  14. // код метрики:
  15. $metrika = '(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
  16.   m[i].l=1*new Date();
  17.   for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
  18.   k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
  19.   (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
  20.  
  21.   ym(99999999, "init", {
  22.        clickmap:true,
  23.        trackLinks:true,
  24.        accurateTrackBounce:true,
  25.        webvisor:true,
  26.        params:{"ip": "'.$ip.'"}
  27.   });';
  28.  
  29. // дальше не нужно ничего менять:
  30. header('Content-Type: text/javascript; charset=UTF-8');
  31. header('Expires: Thu, 18 Aug 1994 05:00:00 GMT');
  32. header('Cache-Control: no-store, no-cache, must-revalidate');
  33.  
  34. $ch = curl_init();
  35. curl_setopt($ch, CURLOPT_URL, 'https://ipscore.biz/api/'.$apikey.'/'.$ip.'/score.json');
  36. curl_setopt($ch, CURLOPT_HEADER, false);
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 600);
  39. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  40. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  41. curl_setopt($ch, CURLOPT_USERAGENT, 'ipscoreclient'); // не изменять user-agent!
  42. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  44. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  45. curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
  46. $outch = curl_exec($ch);
  47. curl_close($ch);
  48. $outch = json_decode($outch, true);
  49.  
  50. if (isset($outch['score']) AND $outch['score'] == 1) {echo $metrika;}
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement