2

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

Marcus
  • 67
  • 2
  • 12
  • Not entirely following. Can you please be more specific with your issue (not annoyances). Nice name, btw. – mferly Apr 20 '16 at 13:19
  • I'm sorry if I was not specific enough. I have a class called active, that underlines stuff when I'm on the site. The php in the post does that. Whenever I'm on the a page, it also shows the "/" page. – Marcus Apr 20 '16 at 13:23

1 Answers1

2

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98