Carl Owens
Carl Owens

Reputation: 1292

php show holding page using ip address

A common problem that I face is placing a holding page on a website, while a new deployment is performed or some other maintenance is done (like testing the new deployment). I know this can be accomplished easily if you are using Apache (htaccess), but this is not always the web server in use (IIS, Nginx, etc). All my current websites redirect every request to an index.php file in the root of the public directory (e.g. Zend Framework, Wordpress, Symfony2), and so my current solution is the following:

To add the following code into the root index.php file:

$maintenanceFile = 'maintenance.flag';

if (file_exists($maintenanceFile)) {
    $ips = explode("\n",file_get_contents($maintenanceFile));
    foreach($ips as $key=>$value) {
        $ips[$key] = trim($value);
    }
    if(!isset($_COOKIE['BYPASS_MAINTENANCE']) && !in_array($_SERVER['REMOTE_ADDR'], $ips)) {
        include_once dirname(__FILE__) . '/holding.html';
        exit;
    }

}

With this I can simply add a maintenance.flag file also in the root, which contains a list of allowed ip addresses like so:

127.0.0.1
123.456.789.101

Then if the your ip address exists within the list, you can see the current website and test it etc before going public again, otherwise you are shown a holding.html page (which also resides in the root) instead. Once finished I can simply delete the maintenance.flag file.

So my question is, is there a better way of doing this?

Upvotes: 2

Views: 214

Answers (2)

lifo
lifo

Reputation: 2893

The development and production versions of a website should always be in different directories and accessed using different domain names or ports or paths. And both should never use the same DB or other resources like uploaded files, etc.

If you're using Subversion, or git or any code repository (which I hope you are) then its easy to have a separate "dev" environment setup where you can do all your testing, etc. All my sites are setup this way. Once I implement or fix features and have tested them in my "dev" side I commit my changes to SVN and then on my "prod" side I simply do an svn update to push everything into production.

The problem with your $maintenanceFile technique is every single page request to your site will result in a file_exists check on your hard drive. This may not matter for smaller sites but for larger sites with a lot of hits this could result in slower performance overall.

Upvotes: 1

Ron van der Heijden
Ron van der Heijden

Reputation: 15070

I split up my website in 3 parts (copies) and have 3 subdomains.

1) dev.website.com is for developers. You can access if your ip is found, like your way.

2) test.website.com is for tester before I launch the website we need to test for buggs.

3) live.website.com equal to website.com for all the others.

This way I can easy develop without causing problems on the live domains.

You can easy switch from dev to test to live. You can copy environments easy.

Hopefully helped you!

Upvotes: 2

Related Questions