Automatically switch from local to remote value of $root variable

I have several websites and I plan to use the same php file to notify my updates in all the sites.
But in this way I have the problem that I can’t use, as I did so far, a relative path, given the fact that my websites are hosted in different remote server.
So I wonder if there is in php a way to switch the $root variable from a (relative) local address (http://localhost) to a remote one (https://myremotewebsite), according to the fact to be called from a local server or from a remote one.
The variable to switch is one: $root. What is the $root value? In localhost something, in remote other.
Thank you.

maybe using (also) CSS: to show or hide a content (according to its “class”).
But using also $_SERVER, I guess… to switch from a class to another…

Http://localhost is not a relative but a absolute path.

You can use DIR to use a real relative path

I have mirror images of the remote sites and define the following :slight_smile:


// usage
$basurl = LOCALHOST 
  ? ‘http://localhost/‘
  : ‘’
echo LOCALHOST ? NULL : ‘view/incs/advert-001.php’;
1 Like

Sorry, but I have found a quite simpler solution: define, in a file linked with my home (where are the links to the updated or added pages) as many $root_other_sites variables as necessary.
F.e. in my website A $root_A is the root folder (regardless for local or remote), and another my website has as root the $root_B variable (the absolute remote path), and $root_A = $root in website A, and in website B $root = $root_B.
It works and with very few code.
Thank you any way!

1 Like

@web148 ,
If you have a mirror image of your online domains yu may be interested in the following utility that creates RSYNC scripts to be copied and pasted into your Command Box:

<?php declare(strict_types=1);

  $aDirs = [
  $rsync = '<dl>';
  foreach($aDirs as $key => $dir) :
    $rsync .= <<< ____EOT
    <dt> <b> $dir </b> </dt>
      rsync -avze ssh /var/www/$dir/  &nbsp; root@123.456.789.42:/var/www/$dir/
    <dd> &nbsp; </dd>  
  $rsync .= '</dl>';

?><!DOCTYPE html><html lang="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Apache2 Ubuntu Default Page: It works</title>
<style type="text/css" media="screen">
* {
    margin: 0; padding: 0;
html {
  padding: 3px;
  background-color: #D8DBE2;
  font-family: Verdana, sans-serif;
  font-size: 1.0em;
  text-align: center;
dl {
  width: 88%; 
  margin: 1em auto; 
  background-color: #ff0; color: #000; 
  text-align: left;
dl dd {margin: 0 1em;}
<h1> RSYNC.php </h1>

<?= $rsync ?>



If your code needs to know on which URL it is running, your code is wrong.

1 Like

Please give examples to your support your argument.

Having code generate a dynamic URL is beneficial instead of hard-coding the URL multiple times especially if the domain is moved from a Sandbox to a live server.

I use the following script that manages to determine the exact domain. It works for localhost, http, https, https://www, and even sub-domains :slight_smile:

# dynamic BaseUrl -  
  $tmpUrl = (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) 
      ('on' == $_SERVER['HTTPS']) 
      ? "https://" 
      : "http://") .$_SERVER['HTTP_HOST']
  $tmpUrl .= str_replace
  defined('BASEURL') ?: define('BASEURL', $tmpUrl); 

1 Like

For what do you need the URL? You can start any script with relative path like

“…/sub/myscript on subfolderinparentfolder”

So you never need a absolute url to run any other script from your page and you do not need to know the root at any time and it works on any server instance doesn’t matter where it is installed

1 Like

This is my code:

in the root of website A

<?php $root=".";

in the root website B

<?php $root=".";

so in the file shared between A and B I can use, as link, $root_A and $root_B as content of their url and each website knows what that variable mean exactly (website A knows that $root_A is its $root, and website B knows that $root_B is its $root).

Point taken for calling scripts from the same site.

Does it also apply to supplying links for users to save or links to files to downloads such as .pdf, .exe, .xls, etc?

I find that confusing because…

Paths never have a https:// prefix.

Using the prefix makes the string into a URL.

To be honest I’m not sure, but why not? I will check.

@web148, what is the real problem you are trying to solve by doing this?

Why not create a third site that only has the updates part and then refer to that site from all other sites?

Seems a lot simpler to me.

Regardless of where you are, you use that same site for the updates. Always.

1 Like

My aim was to have only one (php) file for my several websites to notify the new or updated webpages and to have links (within that php file, pointing to several my websites) working correctly in all my websites.
My solution works.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.