How to create a BASE website address if the menu is in a different folder?

I know that it’s stupid to do, but I have a situation where I create a menu in different folders. And I tried to create baseurl for the menu.

function getBaseUrl() { $currentPath = $_SERVER['PHP_SELF']; $pathInfo = pathinfo($currentPath); $hostName = $_SERVER['HTTP_HOST']; $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://'; return $protocol.$hostName.$pathInfo['dirname']."/"; } 

Use the base url as <?php echo getBaseUrl(); ?> <?php echo getBaseUrl(); ?> . My problem:

Let me have aaa bbb ccc ddd menu. And the aaa menu page is inside folder1/page1.php

The bbb page is inside folder2/page2.php . I cannot go from aaa to bbb menu.

Does anyone have an idea? please suggest

+5
source share
2 answers

You can create an array with a list of pages that you specified using the label:

 function getBaseUrl($naviItem) { var $navigation = array( 'aaa_menu' => '/folder1/page1.php', 'bbb_menu' => '/folder2/page2.php' ); //$currentPath = $_SERVER['PHP_SELF']; //$pathInfo = pathinfo($currentPath); $hostName = $_SERVER['HTTP_HOST']; $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://'; // return the path relative to the passed navitem return $protocol.$hostName.$navigation[$naviItem]; } 

Then in your HTML you pass this shortcut relative to the navigation element that you want to get for baseURL for:

 <a href="<?php echo getBaseUrl('aaa_menu'); ?>">aaa</a> <a href="<?php echo getBaseUrl('bbb_menu'); ?>">bbb</a> 
+2
source

Hi Shrestha, it's very simple to define a constant and use

 define('DOMAIN_URL', 'http://' . $_SERVER['SERVER_NAME'] . '/projectname/'); 

And when setting up links -

 <a href="<?php echo DOMAIN_URL; ?>/folder1/page1.php">aaa</a> <a href="<?php echo DOMAIN_URL ?>/folder2/page2.php">bbb</a> 
0
source

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


All Articles