How to handle relative / absolute path with include

Can somebody say whats the best way to handle includes when having an include like footer.php which is one time in ROOT and onetime in ROOT/subfolder

Should I just add in the include the “subfolder” path on that related page? Or is there another way to handle that?

Freddy, I just put up with the fact that the path for the location of the include file is wrong.

Problem is, that the root directory for the localhost is different to the root directory of the remote host. In other words, having a link like /css/main.css will be interpreted differently on the localhost to what the intention is.

One way out of this would be to make the link

    echo '/'.trim( $_SERVER['DOCUMENT_ROOT'], '/' ).'/css/main.css';

okay thanx @ben thats what I needed! Used it 20 years ago but with all that clicky clicky stuff I completely forgot it!

IN must cases just starting the path with a “/” will make it relative to the root, just ensure your local structure mirrors your online structure.

For example many hosts will use public_html folder or similar for the web content, make sure your local root mirrors that.

Brian, I use wamp, the root folder for wamp is www and not the site root www/mysite.

On the remote, the root folder is www (or equivalent).

yes but I see you understand what issue I may face. Also want to have still a clean development environment with Wappler/Live Preview…

For what is APP Root and Body?

I change the base root of the server to match that of the online system via the DocumentRoot to ensure they remain in sync

App root is the location of the app, the body is it’s content

I use WAMP too. I just create subfolders for each site (which are the site roots) - eg \wamp64\www\mysite - and everything seems to work without any trouble between local and remote versions. Perhaps I’m not using WAMP as intended but it all seems to work as I want.

I don’t bother with any development environment on my local machine at all, I only use remote.

When I need a new site called something like I just go and create a new account in WHM with that domain, regardless of it is is a registered domain name or not. I then use my /etc/hosts file to tell my machine what the IP Address of the server is when DNS looks for

If a client wants me to re develop their existing website like lets say or something then I can do the exact same procedure as above.

This works well as once the client approves the website I have no need to make any alterations to any files at all, i just add another remote to Wappler and upload everything.

Anyway, each to their own, just saves me from setting up web servers, ftp servers, dynamic ip to hostname services so my client can access it, etc.

@psweb yes exactly! Also using that way for preview websites, but didnt try with wappler. Good idea

I do the same (except I combine this with WAMP). For a site you’re redeveloping, using the hosts file is particularly useful as it enables you to see the current version and the version you’re working on at the same time. Eg you can have for one version and for the other.


@TomD completely agree with you. I even think such a feature should be in wappler which edits your hosts file :laughing::ok_hand:t5:. I think its the best „workaround“,… but maybe Antivirus Apps won‘t be happy to see Wappler changing hosts file :smile:

Wappler can already do that on a mac, there is a button on the bottom right saying Terminal, hit that and type in sudo nano /etc/hosts ENTER,
type your machine password ENTER
the hosts file opens in Wappler in the bottom terminal / console section, make your changes, CTRL+o initiates the save function, then ENTER to allow save over the existing file, then CTRL+x to exit the hosts file nano editor.

If you prefer you can also use sudo pico /etc/hosts whichever terminal editor you prefer.

I just tried, following the advice of others, links relative to site root with positive results. This eliminates the need for the PHP code.

Unfortunately, in my case this does not help. I tend to place a proposed site as a sub folder of an existing remote site. When I use the solution stated above, the links will use the existing site as the root folder. I’ll just stick with what I have always done which is to disregard the bad link in the included file.

I dont get it. I’m stuck with the same problem.

@psweb I know your solution, but it would slow down my whole process, cause I have slow Internet here.
@ben your solution would work, but its a quirk for me … :frowning: As I’m overwriting other projects and not having clean structure then. Maybe i could just FAKE my localhost for each project.

like Project 1 would be http://project1/wapplerstuff.php
Project 2 would become then http://project2/wapplerstuffsomething.php

Maybe that you understand my case better I have the following situation:

  1. I have a head.php which includes globally stuff and has paths for favicon etc.
  2. The problem comes now that I’m having subfolders on my project. And sometimes I dont know on which level I’m accessing to my head.php.

Example with subfolder on Remote works fine: which includes a head.php via include("/head.php");

Example with subfolder subfolder on Remote works fine: which includes a head.php via include("/head.php");

Locally that all above doesnt work, cause it wants to access th
e root of localhost.

Example with subfolder on Remote works not of course:
http://localhost/admin/dashboard.php which includes a head.php via include("/head.php")

I mean isnt there a proper way to work with subfolders, when I have different levels of subfolders?