我的脚本如下:
<li><p class="navbar-text"><a href="/" <?php if (stripos($_SERVER['REQUEST_URI'],'/') !== false) {echo 'class="active"';} ?>>Forside</a></p></li>
<li><p class="navbar-text"><a href="/medlemmer" <?php if (stripos($_SERVER['REQUEST_URI'],'/medlemmer') !== false) {echo 'class="active"';} ?>>Medlemmer</a></p></li>
<li><p class="navbar-text"><a href="/butik">Ansøg</a></p></li>
<li><p class="navbar-text"><a href="/" style="color:white;">Køb</a></p></li>
我正在使用 Bootstrap,并且我将此代码放在一个单独的文件中,我从
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/header.php";
include_once($path);
?>
<?php if (stripos($_SERVER['REQUEST_URI'],'/') !== false) {echo 'class="active"';} ?>
让我烦恼。所有其他页面都按其应有的方式执行,但是这个“/”斜杠只占用服务器上的每个页面。
我可以做/index,但这在地址栏中看起来很糟糕。
我该怎么做才能让某些内容仅在前端(index.php)上回显?
最佳答案
parse_url()
可能会帮助你:-
<li><p class="navbar-text"><a href="/" <?php if (parse_url($_SERVER['REQUEST_URI'])['path'] =='/') {echo 'class="active"';} ?>>Forside</a></p></li>
<li><p class="navbar-text"><a href="/medlemmer" <?php if (parse_url($_SERVER['REQUEST_URI'])['path'] =='/medlemmer') {echo 'class="active"';} ?>>Medlemmer</a></p></li>
<li><p class="navbar-text"><a href="/butik">Ansøg</a></p></li>
<li><p class="navbar-text"><a href="/" style="color:white;">Køb</a></p></li>
一个例子:-
<?php
$url = "http://localhost/";
echo parse_url($url)['path'].'<br/>';
$url = "http://localhost/medlemmer";
echo parse_url($url)['path'];
?>
关于php stripos if 页面 ==,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36744990/