Logo
Maintenance Mode
Online Now: 2
Snippet Code
Is your site in maintenance? Let users know by directing them to a maintenance page without them disrupting your maintenance.

Introduction


This script requries 2 functions
1. The function to run maintenance mode
2. The function to get the filename

It is recommended that this file is included on EVERY page of your site in order to
work throughout the entire site.

It is also recommended that this is the very first function in your functions file... 
We don't know why, but sometimes it doesn't work if it is not first.

functions.php


<?php
# maintenance mode
function maintain($mode FALSE){ # $mode either equals TRUE or FALSE
    
if($mode){
        
# if we are in maintenance, require all pages to go to the maintenance page
        
if(filename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){
            
# Replace the location to the loacation of your maintenance page
            
header("Location: http://mysite.com/maintenance.php");
            exit;
        }
    }else{
        
# if we are not in maintenance, don't allow link to maintenance page
        
if(filename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){
            
# Replace the location to the loacation to your home page
            
header("Location: http://mysite.com/");
            exit;
        }    
    }
}
# Run maintenance mode
maintain(); # Leave blank to not be in maintenance mode or use maintain(TRUE);

# get the file name
function filename($url){
    
$pos strrpos($url,'/');
    
$str substr($url,$pos+1);
    return 
$str;
}
?>
 
Snippet Comments

Add Your Comment

termite treatment cost
2011-06-25 18:35:18
Great snip I think I'll end up using this function. Thanks again.
This will definitely get you into a maintenance mode. The process works so well.
shyness
2011-06-01 01:00:09
The widely believed to unveil at wwdc next month may have an important feature that the competition lacks. Instead of forcing users.Thanks for sharing the informative post. Regards. Roy.
bernte
2011-04-13 18:32:17
great script!!!!!! :D but what can i do to allow the admin ip to view the site in maintenance mode?
Michael Grigsby
2011-03-14 00:47:06
I've written a script like this before. What I did was tied it into a MySQL database. This approach gave me more flexibility with the script manipulation and as well as security enhancements.
business
2011-03-02 21:20:31
Great snip I think I'll end up using this function. Thanks again.
Anonymous
2010-12-09 18:15:40
Nice idea but if you are working on a site, you need to be able to get to the site. This will dump everyone, including you to the maintenance page. Needs to be flagged for administrators in some way.
php scripts
2010-09-14 00:35:10
i need to say. i need this script. thank you buddy.
Sam Yong
2009-07-25 00:00:00
The filename() function could have been replaced with PHP's native basename() function, which does the same thing.

Add Your Comment

 
Snippet Tools
Rate this Snippet:

Rate the difficulty level:

Request Snippet Update


Suggested Difficulty Level: Advanced
Current Score: 3.03
Total votes: 254
Total Views: 6663

Other top snippets by admin:

1. JPEG to ASCII Converter
2. Add (th, st, nd, rd, th) to the end of a number
3. Dynamic Page Content From Links
4. AJAX Quickie
5. Simple Image CAPTCHA

Search
Search for:







User Panel

User name:

Password:

Register And Post Your Own Snippets

New Snippets

1.  Shorten text and (4 of 1)

2.  file-based simple guest (0 of 0)

3.  adding an entry (0 of 0)

4.  integer to currency (1.33 of 3)

5.  Aggregate IP addresses (0 of 0)

6.  file handling (0 of 0)

7.  testing system with (0 of 0)

8.  Make an php (1 of 1)

9.  allow download in (5 of 1)

10.  redirect in 3 (0 of 0)

11.  live clock with (0 of 0)

12.  visit counter (3.5 of 2)

13.  contact form with (4 of 1)

14.  show a specific (0 of 0)

15.  The & sign (0 of 0)

16.  static variable in (0 of 0)

17.  multiplication table with (0 of 0)

18.  function with multiplicationt (0 of 0)

19.  php generated menu (0 of 0)

20.  drawing a table (0 of 0)

21.  embedded array (one (0 of 0)

22.  checks the week (0 of 0)

23.  put links on (4.5 of 2)

24.  Directory Image Gallery (0 of 0)

25.  TOS (4 of 1)

Home | Forum | Free PHP Web Hosting | Contact | Terms & Conditions |  
Donate
PHPSnips.com - ©2012 Amethyst Creative