php - 使用当前 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.');
?>
解决方案
向我的一个朋友大喊。
只需获取 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>
推荐阅读
- python - 如何从一百万个文件列表中停止的位置开始代码?
- jquery - 加载列表数据时加载微调器
- mysql - 无法设置关系 phpmyadmin
- asp.net - 使用 LINQ 的动态过滤器(EntityFramework)
- macos - 如何在 MacOS 10.15 中正确使用 EndpointSecurity API?
- bash - 系统中最大的文件并在 unix 中移动
- node.js - 使用串行端口启动电子脚本时出现符号查找错误
- javascript - 鼠标悬停事件处理程序在 d3 中不起作用?
- node.js - AWS Lambda - Runtime.ImportModuleError:错误:找不到模块“jmespath”
- roku - 如何在 Roku 中使用圆形图像视图..?