1

So I I've been creating this HEADER and today I included it in a file which wasn't in the same folder as the other .php files. This resulted in the path from the includes inside the HEADER wasn't the right once.

I was wondering if there was a dynamic pathfinder way to solve this?

In the HEADER I have this

<script type="text/javascript" src="./js/togglemenu.js" defer></script>

and was wondering if it would be possible to create a function which includes the path instead like

<script type="text/javascript" src="dynamicInclude(js/togglemenu.js)" defer></script>

I've tried different things, but can't seem to understand how or even if this is possible.

$current_file_path = dirname(__DIR__);
//Writes include(C:\xampp\htdocs\metrics\website\..\togglemenu.js)
//Want to find 'togglemenu.js' no matter which file this line is included, 
//so I don't have to ../../ my way through every file to find the correct path each time.
include(dirname(__DIR__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'togglemenu.js');

EDIT

Small edit with more information:

These is my files

files

In my website\index.php I have

<?php
    declare(strict_types=1);
    include("./includes/header.php");
?>

<h2>Home site</h2>

This works great.

BUT if I write the same include in my website\404\index.php I get error, because the include actually has to be ../includes/header.php. This is where I was wondering if I could make a function like dynamicPageLoad(header.php) which would return the right file path no matter which of these two files this function was used.

I did try out the comment with src="/js/togglemenu.js", but didn't work for me.

enter image description here

Update

Might have found a way to do this? Feel free to change the code if there is a better way to do this

function get_file_dir($path) {
    global $argv;
    $dir = dirname(getcwd() . '/' . $argv[0]);
    $curDir = getcwd();
    chdir($dir);
    $dir = getcwd();
    chdir($curDir);
    return $dir.$path;
}
include(get_file_dir('\includes\header.php'));
  • Use `src="/js/togglemenu.js"` - will always load from the topmost/root folder of your server – brombeer Nov 04 '21 at 08:35
  • @brombeer I tried this, and made a edit to my question with more information, since this didn't work for me. – Mads Sander Høgstrup Nov 04 '21 at 08:50
  • How are you requesting your index.php in the browser, what is the exact, full URL? Sounds like `website` might not actually be your web root here. – CBroe Nov 04 '21 at 09:07
  • Well it didn't really work for .js files I got the error `Not allowed to load local resource:`, but the full path to lets say a js file can be `C:\xampp\htdocs\metrics\website\js\toggletheme.js` but maybe thats why my function doesn't work? maybe I should somehow change it to automatic find the relative path `website\js\togglemenu.js` ? if thats possible @CBroe – Mads Sander Høgstrup Nov 04 '21 at 09:16
  • Please answer the question that I asked you. – CBroe Nov 04 '21 at 09:21
  • request: `http://localhost/metrics/website/index`, which has `include("./includes/header.php");` but I want to be able to just write `include("/includes/header.php");` or maybe even `include("header.php");` if it's the only file @CBroe – Mads Sander Høgstrup Nov 04 '21 at 09:29
  • Well then the full path to your JS file, from the web root, would obviously be `/metrics/website/js/togglemenu.js`, so that's what you should be using in your script element `src` attribute. – CBroe Nov 04 '21 at 09:35
  • would it be possible to cut the first part, so it only was `website/js/togglemenu.js`? since the `metrics` is just a folder I used to clone the git repo into, and is a name I choose myself, that wont be uploaded when I commit changes. @CBroe – Mads Sander Høgstrup Nov 04 '21 at 09:44
  • If you don't want to handle this via some constant or configuration option, that the different path prefixes for the live vs dev version can be stored into, then you would need to setup up a proper virtual host in your test system, so that `http://sometestdomain.local/` or similar, would point to the `metrics` folder as the web root to begin with. I would generally recommend the latter, because keeping your test setup as close to the "real" thing as possible, is always a good idea. – CBroe Nov 04 '21 at 09:52
  • @CBroe do you know why I cant use include("/metrics/website/js/header.php")? Because defining the whole path might be a solution. It works for including js/scss files, but not with the include() function – Mads Sander Høgstrup Nov 04 '21 at 10:34
  • Because a file system path with a leading slash would refer to the file system root. _"It works for including js/scss files"_ - those do not get "included", but referred to on the client side; and in that case we are not talking about file system paths any more, but about URLs. Make sure you are aware of the difference. – CBroe Nov 04 '21 at 11:21
  • @CBroe so the include should be `include($_SERVER['DOCUMENT_ROOT']."/metrics/website/includes/header.php");` – Mads Sander Høgstrup Nov 04 '21 at 11:35

1 Answers1

0

One possible solution is to use a base tag. This sets the base URL for every href in the page:

<base href="https://www.example.com">

Change it to your base URL. Now you only have to set it once in the page - either set it statically or dynamically. All other relative links (hrefs) are then relative to that base URL.

In a PHP application you would typically create separate configurations for your different environments like live and development. Good thing is you only have to print it once in the page and all relative hrefs will work without a problem.

You should also know to avoid a common mistake: With a base href of https://www.example.com like above all hrefs must have / at the start. If you include the / in the base href you must not have a beginning / in all hrefs.
You can check the network tab in the browser's development tools to see if the URLs work correctly.

If you do not have such a setting, check this answer on how to print the base URL dynamically:
How do I get the base URL with PHP?

Documentation: HTML element base

Peter Krebs
  • 3,831
  • 2
  • 15
  • 29