Jump to content

[SOLVED] if ($_SERVER[PHP_SELF] == ....


Mr_jmm

Recommended Posts

Hi all,

 

I'm trying to write a php script to replace that age old Javascript "uberlink".

For those that don't know of "Uberlink" what I'm doing is using a php script that highlights a link to show you are on that page already.

An example of what I'm using:

if ($_SERVER[php_SELF] == '/aboutus.php') {
$set_link = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"';
} else {
	$set_link = 'title="Find information about the website and it\'s owner"';
	}

 

Which adds relevant html into:

$aboutus = '<a href="aboutus.php" target="_self" '.$set_link .'>About Us</a>';

 

and this works perfectly.....

 

except when it comes to doing the same for a page that is the index page for a folder, eg

if ($_SERVER[php_SELF] == '/portfolio/') {
$set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"';
} else {
	$set_linkp = 'title="Have a look at websites designed by ? ? ? ?"';
	}

 

The page still works correctly, as does the nav menu except no highlighted link, infact, none of the links show any title info. Is it possible to use this code for folders and / or could you please advise the correct method.

 

Many thanks.

James

Link to comment
https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/
Share on other sites

Here is a snippet from a site I'm working on that does just that.  Basically it sets up an array of navigation links, where each element in the array consists of the label, url, and a regular expression to check against the current URL.

 

While displaying the navigation menu, I check the current URL with the regexp for the link; if it matches the regexp I set class="current" for the link, which has appropriate style info in the CSS.

 

    <div id="mainnav">
      <?php
        $MainNav = Array();
        $MainNav[] = Array(
          'url' => '/',
          'label' => 'Home',
          'regexp' => '/^\/$/'
        );
        $MainNav[] = Array(
          'url' => '/software',
          'label' => 'Software',
          'regexp' => '/^((\/software)|(\/ibswin)|(\/webview)).*$/'
        );
        $MainNav[] = Array(
          'url' => '/support',
          'label' => 'Support',
          'regexp' => '/^\/support.*$/'
        );
        $MainNav[] = Array(
          'url' => '/pages/clients',
          'label' => 'Clients',
          'regexp' => '/^\/pages\/clients.*$/'
        );
        $MainNav[] = Array(
          'url' => '/pages/contact',
          'label' => 'Contact',
          'regexp' => '/^\/pages\/contact.*$/'
        );
        $MainNav[] = Array(
          'url' => '/pages/about',
          'label' => 'About',
          'regexp' => '/^\/pages\/about$/'
        );
        echo '<ul>';
        foreach($MainNav as $Nav){
          if($Nav['regexp'] != '//' && preg_match($Nav['regexp'], $this->here)){
            $class = ' class="current"';
          }else{
            $class = '';
          }
          echo "
            <li>
              <a href=\"{$Nav['url']}\"{$class}>{$Nav['label']}</a>
            </li>
          ";
        }
        echo '</ul>';
      ?>
    </div>

<ul class="navlist">
  <li><?php echo $home; ?></li>
  <li><?php echo $aboutus; ?></li>
  <li><?php echo $portfolio; ?></li>
  <li><?php echo $faq; ?></li>
  <li><?php echo $contact; ?></li>
</ul>

$home = '<a href="./" target="_self" title="Return to the websites home page">Home</a>';
$aboutus = '<a href="aboutus.php" target="_self" '.$set_Astate .'>About Us</a>';
$portfolio = '<a href="./portfolio/" target="_self" '.$set_Pstate .'>Portfolio</a>';
$faq = '<a href="faq.php" target="_self" '.$set_Fstate .'>FAQ</a>';
$contact = '<a href="contact.php" target="_self" '.$set_Cstate .'>Contact Us</a>';

 

URLs:

mycompany.net

mycompany.net/aboutus.php

mycompany.net/portfolio/

mycomapny.net/faq.php

mycomapny.net/contact.php

Based off everything you've said so far, the only one that is giving you trouble is the portfolio one.

 

So how about this:

<?php
if (strpos($_SERVER['REQUEST_URI'], 'mycompany.net/portfolio') !== FALSE) {
  $set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"';
} else {
  $set_linkp = 'title="Have a look at websites designed by ? ? ? ?"';
}
?>

Try:

 

<?php
if (preg_match('/^\/portfolio.*/', $_SERVER['REQUEST_URI']) !== FALSE) {
  $set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"';
} else {
  $set_linkp = 'title="Have a look at websites designed by ? ? ? ?"';
}
?>

Would not:

 

<?php
if ($_SERVER[php_SELF] == '/portfolio/index.php') {
$set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"';
} else {
	$set_linkp = 'title="Have a look at websites designed by ? ? ? ?"';
	}

 

work...?

 

PhREEEk

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.