如何在PHP脚本中插入Google Adsense代码?

时间:2016-02-29 23:55:04

标签: php adsense

如何使用PHP回复AdSense代码?以下是我为codeigniter php工作的代码示例。

$adsence = " 

<div class=\"right-inner\">
            <center width=\"96% class=\"img-responsive center-block\">
                            <script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script>

<!-- quiz_net -->
              <ins class=\"adsbygoogle\"
                 style=\"display:block\"
                 data-ad-client=<?php echo $client ?>
                 data-ad-slot=<?php echo $slot ?>
                 data-ad-format=\"auto\"></ins>
              <script>
              (adsbygoogle = window.adsbygoogle || []).push({});
              </script>
            </center>     
</div>";

echo $adsence;

我想用PHP在div中插入adsense代码。我还尝试了htmlentities和striplashes,但广告没有显示。

3 个答案:

答案 0 :(得分:3)

data-ad-client=<?php echo $client ?>

你已经在php解析器中了,不需要再打开它

data-ad-client=$client

修复你那样做的另一个地方

$adsence = " 
<div class=\"right-inner\">
            <center width=\"96% class=\"img-responsive center-block\">
                            <script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script>

<!-- quiz_net -->
              <ins class=\"adsbygoogle\"
                 style=\"display:block\"
                 data-ad-client=\"$client\"
                 data-ad-slot=\"$slot\"
                 data-ad-format=\"auto\"></ins>
              <script>
              (adsbygoogle = window.adsbygoogle || []).push({});
              </script>
            </center>     
</div>";

echo $adsence;

答案 1 :(得分:1)

我添加了我的想法,我希望它对你有用。

如果您有托管站点,则可以在数据库中使用查询,或者您可以使用配置文件来不总是利用源代码。

像:

// config_file.php

$adsense = "0"; // 0 - For unactivate , 1 - For activate
$ads_client_id = "ca-pub-1234567890123456"; // Publisher ID
$ads_slot_id   = "123456789"; // Slot ID
$ads_format    = "auto"; // ADS format - auto (This ad unit can automatically adjust the size of space available on the page.)

//嵌入代码

<?php if($adsense == 1){
echo "<script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script>
<ins class=\"adsbygoogle\"
     style=\"display:block\"
     data-ad-client=\"{$ads_client_id}\"
     data-ad-slot=\"{$ads_slot_id}\"
     data-ad-format=\"{$ads_format}\"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>";
}
?>

答案 2 :(得分:0)

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-1325790438113768",
          enable_page_level_ads: true
     });
</script>