Click to See Complete Forum and Search --> : $_SERVER['PHP_SELF'] navigation question
vicpal25
02-01-2005, 11:30 PM
i am working on the following navigation for a site that reads what direectory you are in and applies a certain style to the navigation depending on what the URL is. Here is my code:
$reqURL = $_SERVER['PHP_SELF'];
if ($reqURL == "/products/index.php") {
$menuitem == "products";
}
if($menuitem == "products") {
print("<ul><li id=\"current\"><a href=\"/products/index.php?UID=$UID\">PRODUCTS</a></li>");
} else {
print ("<li><a href=\"/products/index.php?UID=$UID\">PRODUCTS</a></li>");
}
Does anyone know what Im missing? I do get the right output out of "$_SERVER['PHP_SELF']" but I dont think it is passing the value right into one of the variables. I am stuck! grrrrr..anyone has any advice?!
NogDog
02-02-2005, 12:00 AM
$reqURL = $_SERVER['PHP_SELF'];
if ($reqURL == "/products/index.php") {
$menuitem = "products"; # "=", not "==" <<-----<<<<<
}
if($menuitem == "products") {
print("<ul><li id=\"current\"><a href=\"/products/index.php?UID=$UID\">PRODUCTS</a></li>");
} else {
print ("<li><a href=\"/products/index.php?UID=$UID\">PRODUCTS</a></li>");
}
vicpal25
02-02-2005, 01:12 AM
still not working..it's not passing the value...hummm.
tdwork
02-02-2005, 08:19 AM
is the url actaully saying
/products/index.php
or is it saying
products/
vicpal25
02-02-2005, 10:09 AM
it reads /products/index.php because I echo the value of $reqURL at that point.
ShrineDesigns
02-02-2005, 11:54 AM
the $_SERVER['PHP_SELF'] is an absolute path (ex. c:/Program Files/Apache Group/Apache/htdocs/file.ext), until it is echo'd in a href, src, etc., ex.<?php
echo $_SERVER['PHP_SELF']; // absolute uri
echo "<a href=\"{$_SERVER['PHP_SELF']}\">link</a>"; // relitive uri
?>you can use path_info() (i think that is correct) to list the components of a url or <?php
if(preg_match_all("/\\/(.+)/i", $_SERVER['PHP_SELF'], $matches, PREG_PATTERN_ORDER))
{
print_r($matches);
}
?>
vicpal25
02-02-2005, 12:03 PM
i see that $matches executes and array..how can i match the value of what page im exactly on?
ShrineDesigns
02-02-2005, 12:09 PM
i normal use something like this<?php
$page = basename($_SERVER['PHP_SELF']);
switch($page)
{
case 'index.php':
// ...
break;
case 'page.php':
// ...
break;
// and so on ...
}
?>
vicpal25
02-02-2005, 12:38 PM
the case does pull up the appropiate result.yey! now i need to find out a string comparison function..one that reads only the first directory that is on...so if i had the following URL read /products/napkin/index.html it would read only /products/ i can use strpbrk() for that huh?
ShrineDesigns
02-02-2005, 01:55 PM
try$dirs = preg_split("/(\\/|\\\\\\\\)/i", $_SERVER['PHP_SELF'], NULL, PREG_SPLIT_NO_EMPTY);
print_r($dirs);