douchun1900 2013-05-25 00:24
浏览 16
已采纳

PHP:如何使用includes安全地设置页面上的活动链接

in an earlier post "PHP set active link on page using includes" there was a comment "That is waaaay too hackable". can you explain to me how it is hackable and how i can duplicate this idea with a safer method.

$page_id = $_REQUEST['page_id'];

 <ul>
          <li class="<?php echo ($page_id == "Home" ? "active" : "");?>">
 <a href="/">Home</a>
          </li>
          <li class="<?php echo ($page_id == "Apps" ? "active" : "");?>">
 <a href="#">Apps</a>
          </li>
          <li class="<?php echo ($page_id == "Forums" ? "active" : ""); ?>">
 <a href="#">Forums</a>
          </li>
        </ul>

I am building my first plugin for a website and i need to have links that php sets to active if you are on the page such as home or statistics. so the installation may be at www.website.com/home/plugin/statistics. I need to be able to set the class

statistics to active if they are on the page without knowing how many layers deep in their website my plugin is installed.
  • 写回答

1条回答 默认 最新

  • dongyuluan7494 2013-05-25 00:28
    关注

    A better approach may be something like this:

    In the page files (about.php, index.php, etc.) provide the unique identifier at the very top of the page:

    <?php
    $page_id = 'home';
    require_once('_menu.php');
    

    Separate your navigation into a file _menu.php to be included.

     <ul>
              <li class="<?php echo ($page_id == "Home" ? "active" : "");?>">
     <a href="/">Home</a>
              </li>
              <li class="<?php echo ($page_id == "Apps" ? "active" : "");?>">
     <a href="#">Apps</a>
              </li>
              <li class="<?php echo ($page_id == "Forums" ? "active" : ""); ?>">
     <a href="#">Forums</a>
              </li>
            </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集