Search code examples
phptwitterpreg-matchstrposhttp-referer

Displaying Google AdSense Advertisements only Twitter Visitors


I want to display my Google Adsense ads only when visitors come from Twitter. Currently I'm using this code this is working for Facebook and others but its not working on Twitter

    <?php
            $ref = $_SERVER['HTTP_REFERER'];
            if (strpos($ref, 'twitter.com') != false) {?>
 <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>
</div>

<?php }else{
  echo "";  }
  ?>

Solution

  • Twitter redirects links via https://t.co
    look for a t.co referer