>Newb在这里致力于完善功能以提供更好的用户体验。
我的目标是,如果我们不在网站的索引页(主页(上,我希望包含一个返回主页的链接。但是如果我们在索引页(主页(上,我不希望在菜单中显示冗余链接。下面是我构建的函数,你可以在这里看到"school.max-o-matic.com"。右侧导航菜单的底部是一个链接,上面写着"<返回",该链接不应显示在索引页面上,但理想情况下应显示在所有其他页面上。>
我希望使用感叹号后跟等号可以解决问题,但事实并非如此。
<?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') {