How can one set relative paths for links when including a file from a different directory that contains links?

My project directory is structured as follows:

root 
  css
  img  
  src
    login 
       login.php
    dashboard
          basic
              header.php
              footer.php
              profile.php
              manage.php 
          department
              add_depart.php
    configuration
          config.php

The header.php file includes profile.php, manage.php, add_depart.php, and config.php.

In the header.php, there are links that point to other files kept in the basic folder. My issue is that when I include header.php in add_depart.php and config.php, the links do not work. I understand that using include only copies the file into add_depart.php and therefore, the links cannot be accessed within the department folder.

How can I set a relative path for the links inside header.php so they can access the links provided by the header from the basic, department, and configuration folders?

I have also tried using getcwd() and dirname(__FILE__).

Please help.

Header.php

$path_r = $_SERVER['DOCUMENT_ROOT'];
$path_l = $path_r . '/root/src/dashboard/basic/';
 <li class="dropdown">
    <a href="#" data-target="#" class="dropdown-toggle" data-toggle="dropdown"> Settings <b class="caret"></b></a>
    <ul class="dropdown-menu">
        <li><a href="<?php echo $path_l ?>/profile.php">Profile</a></li>
        <li><a href="<?php echo $path_l ?>/config.php">Config</a></li>
        <li class="divider"></li>
        <li><a href="logout.php">Logout</a></li>
    </ul>
</li>

Answer №1

You seem to be mixing up file system paths with URL paths.

$_SERVER['DOCUMENT_ROOT'] actually provides the absolute path to the document root on your server (in the file system), not the URL. The same applies to getcwd() and dirname().

To avoid confusion, always start URLs with a slash:

<a href="/path/to/profile.php"
, etc., so that the URLs remain consistent regardless of where they are included.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The script for tracking cursor coordinates is incompatible with other JavaScript code

<html> <script language="javascript"> document.onmousemove=function(evt) { evt = (evt || event); document.getElementById('x').value = evt.clientX; document.getElementById('y').value = evt.clientY; document.ge ...

Utilizing browser local storage in web development

Currently, I am in the midst of working on an e-commerce platform, a project that holds significant importance for me as it marks my debut into major projects. For the first time, I am delving into the realm of local storage to manage basket data such as q ...

Sorting and breaking a while loop based on a variable in PHP

I'm facing an issue that has been puzzling me for the past two days. As a newbie in php, I am struggling to understand why my while loop is returning multiple select fields for each option within a select field. For example, when I have a "choose colo ...

What is the best way to choose an Animatedmodal that can showcase various demos while using just one ID?

I am currently customizing my website and utilizing the AnimatedModal.js framework. I have successfully displayed content in one modal, but encountered difficulty when attempting to create multiple modals as they all share the same ID. My question is, how ...

Use CSS3 to conceal the form while displaying the default div

In my project, I am restricted to using only HTML5 and CSS3. Currently, I have a form that requires simulating the action of "sending form data". Upon clicking the submit button, the form should be hidden and a default div (#result) needs to be displayed. ...

Implementing websocket functionality in Yii framework

I am currently working on integrating websocket functionality into my web application using PHP and as an extension for Yii. This will allow me to create a notification system similar to what I have in mind. My starting point is the code available at the ...

PHP - Working with Regular Expressions in Improperly Formatted

Struggling with Regex has always been a challenge for me, especially when I need to extract content from a malformed piece of code like the option tag. The website where this code is obtained from has poor programming and inconsistent label structures: I& ...

Can file data be used to obtain a resource handle?

I am currently working on a function that reads data from a csv file and returns an array containing the data. My goal is to modify this function so that it can accept the file data directly instead of the file itself. I would like to refactor the code be ...

Adjusting the content of mat-cards to fill in blank spaces

I have encountered an issue with the alignment in a list using mat-card. Here is my current layout: https://i.stack.imgur.com/VKSw4.jpg Here is the desired layout: https://i.stack.imgur.com/8jsiX.jpg The problem arises when the size of content inside a ...

Show pictures featuring a shared specific attribute? (WordPress)

I'm currently working on creating a slider for my website, and I've hit a bit of a roadblock. I want to avoid using plugins as much as possible (I prefer to learn how to do things myself, and most plugins come with extra features that I don' ...

Steps to update a JSON file when running a query:

Is there a way to update the json file results.json without having to execute the demo.php file? demo.php <?php header("content-type:application/json"); require_once("dbConnect.php"); $sql = "SELECT id ,,fullname ...

jQuery: Issue Encountered with POST Request when Offline (iOS & Chrome)

After developing an HTML5 web application with offline capabilities using AppCache, the process flow is as follows: Online: While connected to the network, the app loads base information in advance. Offline: Users can take the app offline on their tablet ...

Customize the default classes for DataGrid in Material UI

Attempting to customize the CSS properties in the "DataGrid" Material UI, specifically adjusting font weight and overflow of the header. Below is the JSX code snippet: import React, {useState, useEffect} from 'react'; import {DataGrid, GridToolb ...

Tips on eliminating the gap between two flex items while adjusting their size

Can anyone assist me with my code? Here is the current version: https://i.stack.imgur.com/00RGC.png I want it to resemble this: https://i.stack.imgur.com/JSclD.png You can find all my attempted media queries in the CSS comments at the end. Please let m ...

The dropdown menu in the bootstrap is not being properly filled with content on the webpage

In my header file, I am attempting to add a bootstrap dropdown that will display the user id along with two other options in the dropdown menu. Within the header file, I have created a new div element for the dropdown and linked it to a php file where the ...

Switch up the placement of articles, going back and forth between left

On the page, I want to display articles with alternating positions - one on the left, the next on the right. In this example, all the articles are currently being displayed twice, both on the left and the right side. How can I ensure that only one instanc ...

Activate an iframe upon changing the media query

I am currently working on a webpage where I have included an image within an iframe. I am looking to change the content displayed inside the iframe based on different media query breakpoints. For instance, if the viewport has a minimum width of 900px, I ...

Ensuring JS consistently monitors changes in value

Is there an equivalent of (void update) in Unity that is called every frame in Web Development (using JavaScript)? "I want it to continuously check if certain values have changed and then update them accordingly." let governmentprice = parseFloat(select ...

How about this: "Unveil the beauty of dynamically loaded

var request = new Request({ method: 'get', url: 'onlinestatusoutput.html.php', onComplete:function(response) { $('ajax-content').get('tween', {property: 'opacity', duration: 'long&apos ...

Update the button text dynamically when clicked without using an identifier or a class

If we take a look at my button in the following code: <input type="button" value="BLUE" name="button_blue" /> My goal is to have the value="BLUE" changed to value="RED" or any other desired value when the button is clicked. ...