HomeDownloadsSynchronizing Production and Development Websites

software times™Downloads...

March 4, 2013

Synchronizing Production and Development Websites


I develop websites on a Macintosh laptop and host them on a LAMP server. There are differences in the file system which require some small tweaks to make the code run smoothly on both environments.

PHP Includes

On the production server I like to place my php code above the public area, above public_http, to keep it safe from hackers. On the Mac where I develop it is more convenient to have the includes in the public directory. To be able to upload and download seamlessly between production server and development PC the code has to detect in which environmemt it is running in and to fetch the includes accordingly.

<?php
if ($_SERVER['HTTP_HOST'] == 'softwaretimes.com') {
$include = "/home/user/includes/";
} else {
$include = "/Users/user/Sites/softwaretimes.com/includes/";
}


Menus, CSS and Javascript

On the production server the base for every domain is the public_http directory but on the Mac where I work with multiple domains each domain has its own folder and its base is one level below the public_http (Sites) directory. The solution I have been using is to add a base html tag to the development code:
<base href="http://127.0.0.1/~user/softwaretimes.com/">

While it works the uploading of code is no longer seamless. A better solution is to make the links relative letting the code figure out the relation. This is the new code I'm using:

<?php
################################################################
# #
# depending on how you organize your files you might have to #
# adjust the offsets in #
# $count = count($scriptFilename) - $foundOSX - 3; #
# and in #
# $count = count($scriptFilename) - $foundLAMP - 2; #
# #
################################################################

function offset2root() {

$scriptFilename = explode('/', $_SERVER['SCRIPT_FILENAME']);
$foundOSX = array_search('Sites', $scriptFilename);
$foundLAMP = array_search('public_html', $scriptFilename);
if(
$foundOSX !== FALSE) {
$count = count($scriptFilename) - $foundOSX - 3;
} elseif (
$foundLAMP !== FALSE) {
$count = count($scriptFilename) - $foundLAMP - 2;
} else {
return
'./';
}

$offset = '';
while (
$count-- > 0) {
$offset .= '../';
}
if(empty(
$offset)) {
return
'./';
} else {
return
$offset;
}

}

$link = offset2root();
echo
"<a href='{$link}path/to/file.php'>Menu Item</a>";

How It Works

The code calculates the offset between the domain root and the file calling the function. On a LAMP server the domain html base is at "public_html":
/home/user/public_html/some_folder/another_folder/script.php
On an OSX Mac the domain html base is at "Sites":
/127.0.0.1/~user/Sites/some_folder/another_folder/script.php
First the function finds the domain html base: "public_html" or "Sites"

$scriptFilename = explode('/', $_SERVER['SCRIPT_FILENAME']); 
$foundOSX = array_search('Sites', $scriptFilename);
$foundLAMP = array_search('public_html', $scriptFilename);
Next the function calculate the offset between the domain html base and calling script (minus an adjustmet as needed). Either
$foundOSX or $foundLAMP
has the location of the domain html base while
count($scriptFilename)
has the location of the calling script.

If a base is not found the function returns './' (no offset):

if($foundOSX  !== FALSE) { 
$count = count($scriptFilename) - $foundOSX - 3;
}
elseif ($foundLAMP !== FALSE) {
$count = count($scriptFilename) - $foundLAMP - 2;
}
else {
return
'./';
}

If a base is found the function returns the proper number of '../' to reach the domain html base:

$offset = ''; 
while ($count-- > 0) {
$offset .= '../';
}
if(empty(
$offset)) {
return './';
}
else {
return $offset;
}

To see it more graphically add the following to the top of the function. It will show the array the function works with:

echo "_SERVER['SCRIPT_FILENAME']= {$_SERVER['SCRIPT_FILENAME']}<br>\n";
$scriptFilename = explode('/', $_SERVER['SCRIPT_FILENAME']);
echo
"scriptFilename= \n<pre>\n";
print_r ($scriptFilename);
echo
"</pre>\n";
The reason I use the extra offset on the Mac is because I work on multiple domains
/127.0.0.1/_user/Sites/domain-1.com/
/127.0.0.1/_user/Sites/domain-2.com/
/127.0.0.1/_user/Sites/domain-3.com/
making each domain's base one deeper than "Sites.'

Denny Schlesinger


Share this article with your followers

Top
HomeDownloadsSynchronizing Production and Development Websites
Copyright © Software Times, 2000 - 2013. All rights reserved.
Last updated March 4, 2013.