Multiple Development Environments on One Domain

While it’s true that distributed development should be managed by source control, with each participating party pushing or issuing pull requests from within their own environments, this is sometimes not possible.

Participants might not be able to setup their own development environments in light of many possible reasons and limitations. So remote development environments are usually setup for such participants so that they can work on their own forks of a project without interfering with others’ work. S/FTP/S is usually used, due to their inability to use version control remotely.

Here’s an interesting strategy that came to mind that allows participants to have their own environments on one single domain, single server, with the ability to switch between them without having to edit their hosts files or any other magic.

Cookies. Depending on a cookie key-value pair a different document root is served up. The cookie can be set using the setcookie via some simple interface like:

<?php
    /**
        Allows us to switch between document roots
        fairly easily by keeping track of a cookie
    */

    if ( isset( $_REQUEST['env'] ) ) {
        setcookie( 'env', $_REQUEST['env'], null, '/' );
        $_COOKIE['env'] = $_REQUEST['env'];
        header( 'Location: https://' . $_SERVER['SERVER_NAME'] );
        exit();
    }
?>
<p>Current: <?php echo htmlentities( isset( $_COOKIE['env'] ) ? $_COOKIE['env'] : 'undefined' ); ?></p>
<form method="POST">
    <label for="env">Switch to:</label>
    <select name="env">
        <option value="development">development</option>
        <option value="development-gennady">development-john</option>
        <option value="development-peter">development-amy</option>
    </select>
    <input type="submit" value="switch">
</form>

This cookie can then be read by nginx to serve up a specific document root, keeping each root separated:

    set $root /home/www/mysite;
    if ( $http_cookie ~ "env=development-john$" ) { set $root /home/john/mysite; }
    if ( $http_cookie ~ "env=development-peter$" ) { set $root /home/peter/mysite; }
    root $root;
    index index.php;

Both can work simultaneously over S/FTP/S without fear of overwriting. Someone would then commit and merge their work upstream (until they learn to do so themselves, in a perfect world). Quite intricate, don’t you think?