【问题标题】:PHP Includes, including Social buttonsPHP 包括,包括社交按钮
【发布时间】:2012-08-16 13:04:11
【问题描述】:

我在我网站的每个页面上都使用了它,如下所示,以避免在 Firefox 中查看网站时显示社交按钮:

<?php
if (!empty($_SERVER['HTTP_USER_AGENT']))
{
$isFirefox = (strpos($_SERVER['HTTP_USER_AGENT'],"Firefox") !== false);
} else {
$isFirefox = false;
}
if ($isFirefox) { echo ' '; }
else
include("/includes/social_buttons.php");?> 

我的

social_buttons.php

包含以下代码:

<!--=================== social buttons ==================-->
<div id="social">
<!-- Google +1 button -->
<div style="float:right;padding-top:1px;">
<script type="text/javascript">document.write('<div class="g-plusone" data-size="tall"><\/div>');</script>
<script type="text/javascript">
(function() {
var z = document.createElement('script'), s = document.getElementsByTagName('script')[0];
z.type = 'text/javascript';
z.async = true;
z.src = 'https://apis.google.com/js/plusone.js';
s.parentNode.insertBefore(z, s);
})();
</script>
</div>
<!-- End Google +1 button -->

<!-- Facebook button -->
<div style="float:right;padding-right:2px;">
<script type="text/javascript">
//<![CDATA[
document.write('<div id="fb-root"><\/div><fb:like href="<?php echo $url;?>" send="false" layout="box_count" width="35" show_faces="false" font="arial"><\/fb:like>');
(function() {
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
s1.parentNode.insertBefore(s, s1);
})();
//]]>
</script>
</div>
<!-- End Facebook button -->

<!-- Tweet Button -->
<div style="float:right; margin-right:11px;">
<a href="http://twitter.com/share?via=youraccount&amp;count=vertical" class="twitter-share-button">Tweet</a>
<script type="text/javascript">
var a = document.createElement('script'), b = document.getElementsByTagName('script')[0];
a.type = 'text/javascript';
a.async = true;
a.src = 'http://platform.twitter.com/widgets.js';
b.parentNode.insertBefore(a,b);
</script>
</div>
<!-- End Tweet Button -->
<!-- end #social --></div>
<!--=================== end.social buttons ==================-->

现在的问题是,我想在我的 social_buttons.php 中添加一些额外的内容,这些内容将显示在包括 firefox 在内的每个浏览器中。

所以我的尝试(这没有奏效!)是把它全部放在我的 social_buttons.php 中,然后用更简单的包含调用来调用它

<?php include("/includes/social_buttons.php");?>

因此 social_buttons.php 的内容看起来像这样:

<?php if (!empty($_SERVER['HTTP_USER_AGENT']))
    {
    $isFirefox = (strpos($_SERVER['HTTP_USER_AGENT'],"Firefox") !== false);
    } else {
    $isFirefox = false;
    }
    if ($isFirefox) { echo ' '; }
    else
    echo ("
    <!--=================== social buttons ==================-->
    <div id="social">
    <!-- Google +1 button -->
    <div style="float:right;padding-top:1px;">
    <script type="text/javascript">document.write('<div class="g-plusone" data-size="tall"><\/div>');</script>
    <script type="text/javascript">
    (function() {
    var z = document.createElement('script'), s = document.getElementsByTagName('script')[0];
    z.type = 'text/javascript';
    z.async = true;
    z.src = 'https://apis.google.com/js/plusone.js';
    s.parentNode.insertBefore(z, s);
    })();
    </script>
    </div>
    <!-- End Google +1 button -->

    <!-- Facebook button -->
    <div style="float:right;padding-right:2px;">
    <script type="text/javascript">
    //<![CDATA[
    document.write('<div id="fb-root"><\/div><fb:like href="<?php echo $url;?>" send="false" layout="box_count" width="35" show_faces="false" font="arial"><\/fb:like>');
    (function() {
    var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
    s1.parentNode.insertBefore(s, s1);
    })();
    //]]>
    </script>
    </div>
    <!-- End Facebook button -->

    <!-- Tweet Button -->
    <div style="float:right; margin-right:11px;">
    <a href="http://twitter.com/share?via=youraccount&amp;count=vertical" class="twitter-share-button">Tweet</a>
    <script type="text/javascript">
    var a = document.createElement('script'), b = document.getElementsByTagName('script')[0];
    a.type = 'text/javascript';
    a.async = true;
    a.src = 'http://platform.twitter.com/widgets.js';
    b.parentNode.insertBefore(a,b);
    </script>
    </div>
    <!-- End Tweet Button -->
    <!-- end #social --></div>
    <!--=================== end.social buttons ==================-->
    ");?>

    SOME OTHER CODE I WANT TO INCLUDE

有什么办法吗?

【问题讨论】:

  • 究竟是什么没用?是问题所在:它们不显示或不工作?
  • 只需对 social_buttons.php 进行is_firefox 测试,并使用它来确定显示的内容。这里有什么问题?
  • 您还应该确保您的嵌套引号被转义。
  • 你严重搞砸了你的代码。有很多语法错误。你混淆了'",你的echo 里面有&lt;?php echo $url;?&gt;。您需要先清理代码。您不能将所有内容都包含在 echo 语句中。

标签: php javascript html server-side-includes


【解决方案1】:

heredoc 呢?

if ($isFirefox)
{ 
  echo ' ';
}
else
{
  $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

  echo <<<BLAH
  Hello World! I'm $name. <a href="$url">This is a link to this site</a>
BLAH; // important: this keyword has to be at the beginning of a line (do not add any char before this keyword)

}

或者转义你的javascript(双引号)

【讨论】:

  • 谢谢你的回答,但我对php一无所知,所以如果你问的不是太多,如果你能给我提供完整的代码,可以放入我的包含文件中,我将不胜感激。
  • 我已经按照你的建议清理了代码,除了我有 的部分之外,一切都很好。这是我在网站的每个页面上使用的 code code 在规范标签中获取页面的 url,并将相同的 url 复制到 facebook 按钮中。有什么办法让它工作吗?
  • @Bensnik 我已尝试使用您提出的建议,但仍然无效。它打印出 href="$url"
  • 我想我已经做到了,我所做的是 href="'.$url.'",现在我不知道这是否是正确的做法,但至少它有效并且完全按照我的预期工作。感谢@Mathieu Imbert 将我引向正确的方向并感谢贝斯尼克,他曾试图提供帮助。
猜你喜欢
  • 1970-01-01
  • 2021-12-09
  • 2018-06-27
  • 2011-04-29
  • 2021-12-22
  • 2021-06-13
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多