首页 > 解决方案 > 使用当前 URL 参数来操作导航菜单

问题描述

我正在开发一个仅使用 PHP 的小网站。

是否可以使用用户用于访问网站的 URL 参数更改菜单链接?

用户使用此 url 访问网站:

http://localhost/subpages/index.php?page=timeline&name=kay

因此,例如,如果用户名是 mark,则菜单应更改为 aboutmark 而不是 aboutkay。

    <html>
      ..
        <li class="nav-item" role="presentation"><a class="nav-link" href="profilepages/index.php?page="<? 'about'.$name ?> Über mich</a></li>
        <li class="nav-item" role="presentation"><a class="nav-link" href="profilepages/index.php?page="<? 'timeline'.$name ?> Timeline</a></li>
        <li class="nav-item" role="presentation"><a class="nav-link" href="profilepages/index.php?page="<? 'friends'.$name ?> Friends</a></li>
        <li class="nav-item" role="presentation"><a class="nav-link" href="profilepages/index.php?page="<? 'photos'.$name ?> Fotos</a></li>
      ..
    </html>

我更改网站主要内容的代码如下所示:

<?
    if ((isset($_GET['page'])) && (isset($_GET['name'])))
               {
                 $page = $_GET['page'];
                 $name = $_GET['name'];
               }
                else
                    {
                      $page = 'test';
                      $name = 'name';
                    }

     if ((preg_match('/^[a-z0-9\-]+$/', $page)) && (preg_match('/^[a-z0-9\-]+$/', $name)))
               {
                    $inserted = include('profilepages/'.$page.$name.'.php');

                    if (!$inserted)
                    echo('Requested page was not found.');
                }
                 else
                    echo('Invalid parameter.');
                ?>

标签: phpurlparametersnavigationbarisset

解决方案


向我的一个朋友大喊。

只需获取 URL 的 var ...name=var

通过

<?php $name = $_GET['name'];?>

并使用<?php echo $name ?>

像这样:

<li class="nav-item" role="presentation"><a class="nav-link" href="profilepages/index.php?page="<?php echo $name ?> Über mich</a></li>

推荐阅读