What is the best way to get URL Reference using php?

I am trying to set active classes in nav using php. Now I need to install it according to the directory, and not the full URL, since I have a main landing page for each directory with additional navigation for other pages in the directory. I was going to use the following, but this returns the full URL.

function curPageURL() {
$pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

return $pageURL;}

 function activeLink($pageID) {
if ( $pageID == curPageURL() ) {
    echo 'class="active"';
} }

Then I call activeLink () on a navigation element like this:

<li class="projectchild padding1"><a href="http://www.strangeleaves.com/nexus/index.php" <?php activelink('http://www.strangeleaves.com/nexus/index.php'); ?> >nexus</a></li>

Your suggestions on how to change this to return the catalog would be greatly appreciated.

+3
source share
2 answers

If you want to extract directory namefrom URL, you can use parse_urland dirnamehow:

$url = 'http://www.strangeleaves.com/nexus/index.php';
$arr = parse_url($url);
$path = dirname($arr['path']); // $path is now /nexus
+1

URL-, , , :

$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode('/', $currentFile);
$thisDir = $parts[count($parts) - 2];
0

Source: https://habr.com/ru/post/1766440/


All Articles