PHP隐藏特定页面上的div

问题描述 投票:0回答:3

已经搜索了有关此问题的所有主题,但没有一个答案对我有帮助,所以我发布了一个新问题。

我的页面index.php有php包含所有导航链接在同一index.php中打开。例如,当我点击关于我们时,它会打开index.php?page = aboutus。

我的问题是我的主页上有一个div:

<div class="icons_small"> 
<a href="#"><img src="images/layout/facebook_icon_s.png" width="75" height="67" /></a>
<a href="#"><img src="images/layout/twitter_icon_s.png" width="75" height="67" /></a>
<a href="#"><img src="images/layout/youtube_icon_s.png" width="75" height="67" /></a>
<a href="#"><img src="images/layout/location_icon_s.png" width="75" height="67" /></a>
</div>

对于这个div,我希望他隐藏在index.php中,但我希望它在所有其他页面上都可见,如index.php?page = aboutus,index.php?page = contact等。

有什么简单的方法用PHP做到这一点? :)

Thx提前。

php html show-hide
3个回答
1
投票

试试这个。如果没有设置GET参数page或没有值,它将隐藏div。

<?php
if(isset($_GET['page']) && $_GET['page'] != "") {
?>
<div class="icons_small"> 
<a href="#"><img src="images/layout/facebook_icon_s.png" width="75" height="67" /></a>
<a href="#"><img src="images/layout/twitter_icon_s.png" width="75" height="67" /></a>
<a href="#"><img src="images/layout/youtube_icon_s.png" width="75" height="67" /></a>
<a href="#"><img src="images/layout/location_icon_s.png" width="75" height="67" /></a>
</div>
<?php
}
?>

0
投票

这取决于页面的包含方式。

如果我们使用PHP的echo打印出HTML,我们可以简单地选择在没有设置页面的时候用echo打印那个特定的部分(也就是说,没有page=whatever)。

if(!isset($_GET['page'])
{
    echo 'The contents of the div that shouldn't be on the page';
}

如果由于某种原因你不能有选择地打印部分,一个混乱的替代方案是回应一些JavaScript将删除div。但是,这有一个至关重要的缺陷,即仍然会为禁用JavaScript的用户显示div。


0
投票

这根本不安全。我理解这一切都是关于4张图片,但从不良做法中学习对其他项目来说可能是致命的。

如果我写"index.php?page=I_WANT_TO_SEE_THAT_BOX"会发生什么?

我建议的是,检查你是否应该直接在你的包含功能中显示它。我假设你有类似的东西

function IncludeView() {
  $allowed_pages = array("view","friends","foo","bar");
  if(isset($_GET['page'])) {
    if(in_array($_GET['page'],$allowed_pages)) {
      include_once(PAGE_DIR."/".$_GET['page'].".php");  
      define("IS_PAGE_OKAY",true);
    }
  }
  else header(BASE_URL);
}
IncludeView();

稍后在您的模板中:

<?php if(defined("IS_PAGE_OKAY")) { ?>
   //your html code that is shown only on other pages than index.php
<?php } ?>
© www.soinside.com 2019 - 2024. All rights reserved.