When updating or modifying your site, it can be extremely useful to have a function which temporarily redirect your readers to a maintenance page.
Here is a snippet to do it easily.
function maintenance($mode = FALSE){
if($mode){
if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){
header("Location: http://example.com/maintenance.php");
exit;
}
}else{
if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){
header("Location: http://example.com/");
exit;
}
}
}
Usage
In order to activate the maintenance mode, you only have to pass TRUE to the function:
maintenance(TRUE);
Due to the use of the header() function, always make sure that the function is called before any actual output is sent.

3 Comments
Thanks for the code.
I’d prefer to use a htaccess for a maintenance mode in my sites. Here is a sample code – http://pastebin.com/0eReY6Xt
I generally use .htaccess as well, but I like this php solution as well. Thank you for sharing.
Or if you use a framework, you could add it to the router… if $config['maintenance'] === true then go to maintenance page;
2 Trackbacks
[...] This post was mentioned on Twitter by Jean-Baptiste Jung. Jean-Baptiste Jung said: RT @phpsnippets: New PHP Snippet published: Easy “Maintenance Mode” with PHP http://bit.ly/aKATEB #php [...]
[...] == 'maintenance.php'){ header("Location: http://example.com/"); exit; } } }Source: http://www.phpsnippets.info/easy-maintenance-mode-with-phpPrevent js and css files from being cachedBy default, external files such as javascript and css are [...]