Category: Coding

Simple script for viewing the same site from different URLS

EDIT: New version at https://github.com/sabas/magicpath
This php script enables working on the same website without wondering about the location from which you view it.
For example, you have the website files in a folder of your local webserver, and when you put it online, you must access it from another host name.
Examples:
http://localhost/mywebsite/index.php
http://192.168.0.100/mywebsite/index.php (accessing your local machine from another computer on the same network)
http://www.mywebsite.org/index.php (your website on the remote webserver).

This script can be placed everywhere after it’d properly configured (the folder in which the page is must be set).

<?php 
//common: folder containing this file relatively to the website root
$conflen=strlen('common'); 
$B=substr(__FILE__,0,strrpos(__FILE__,'/')); 
$A=substr($_SERVER['DOCUMENT_ROOT'],strrpos($_SERVER['DOCUMENT_ROOT'],$_SERVER['PHP_SELF']));
$C=substr($B,strlen($A));
$posconf=strlen($C)-$conflen-1;
$D=substr($C,1,$posconf); 
$host='http://'.$_SERVER['SERVER_NAME'].'/'.$D;
?>

Note: this works bot on IIS based webservers and Unix based.

Published also on php.net