[英]php: Remove/show specific link from menu based on page displaying
Newb在这里致力于改进功能以提供更好的用户体验。
我的目标是,如果我们不在网站的索引页面(主页)上,我希望包含一个返回主页的链接。 但如果我们在索引页面(Home)上,我不希望在菜单中显示冗余链接。 下面是我建立的功能,你可以在这里看到'school.max-o-matic.com'。 在右侧导航菜单的底部是一个读取“<back”的链接,该链接不应显示在索引页面上,但理想情况下应显示在所有其他页面上。
我希望使用感叹号后面跟一个等号就可以了,但事实并非如此。
<?php
function makeNav($navItem) {
//created variable - plops in what is called when function used on
//page calling the function itself - this is like the strPromp
$output = ''; //Variable of 0 length
foreach ($navItem as $pageLink => $displayedLink) {
if ($pageLink == THIS_PAGE) {
//url matches page - add active class
$output .='<li class="active">'
. '<a href="' . $pageLink . '">' . $displayedLink . '</a>'
. '</li>' . PHP_EOL;
} //PHP_EOL is php line end for all systems
else {//don't add class
$output .='<li>'
. '<a href="' . $pageLink . '">' . $displayedLink . '</a>'
. '</li>';
}
}
if ($pageLink != 'index.php') {//add back button to index page
$output .='<li>'
. '<a href="./index.php">< Back</a>'
. '</li>' . PHP_EOL;
} //PHP_EOL is php line end for all systems
$output .='<li>'
. '<a href="#contact">contact</a>'
. '</li>';
return $output;
}
?>
if($_SERVER['PHP_SELF'] != '/index.php'){ //process if file is not index file.
}
排除对索引文件的访问。
if (strpos($_SERVER['PHP_SELF'],'index.php') !== false) { //process if file contains text "index.php" in filename.
}
//排除对名称中包含“index.php”文件的任何文件的访问
if (basename($_SERVER['SCRIPT_NAME']) != 'index.php'){
//run on all files that ARE NOT index files in any folders.
}
您可以使用$_SERVER
全局变量来检查这一点,该变量是一组请求信息。 密钥“SCRIPT_NAME”包含所请求文件的路径。 检查可能是:
if (basename($_SERVER['SCRIPT_NAME']) != 'index.php') {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.