phphtmltwitter-bootstrap-3stripos

php stripos if page ==


I have the script as below:

<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>

Im using Bootstrap and I have this code in a seperate file, i get from

<?php
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/header.php";
   include_once($path);
?>

The <?php if (stripos($_SERVER['REQUEST_URI'],'/') !== false) {echo 'class="active"';} ?> annoys me. All the other pages does as they should, but this "/" slash just takes every page on the server.

I could do /index, but that would look awful in the address bar.

What can I do to have something echoing ONLY on the front-side (index.php)?


Solution

  • parse_url() may help you:-

    <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>
    

    One example:-

    <?php 
        $url = "http://localhost/";
        echo parse_url($url)['path'].'<br/>';
        $url = "http://localhost/medlemmer";
        echo parse_url($url)['path'];
     ?>
    

    Output:- https://eval.in/556980