A Better Maintenance Mode in Magento?

Featured Image

Magento 1.4+ includes a nice feature that allowed you to throw the store into “maintenance mode” if you need to do/work or make changes. Which is great, freeze people out of the store while you work or make changes. The only problem with this, is it also freezes YOU out of the store so you can’t make the changes you need to. So here is a little bit of code, that allows you and other you set to work on the site while everyone else sees its maintenance mode.

All we need to do it edit 3 lines.

Open: index.php in root and above line 57 add (remembering to edit the ‘allowed’ array to contain the IP’s you want to be able to access the site);

$ip = $_SERVER['REMOTE_ADDR'];
$allowed = array('1.1.1.1','2.2.2.2'); // these are the IP's that are allowed to view the site.

then change the line
if (file_exists($maintenanceFile)) {
to
if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) {

Simple. Now you can access the site, while others see its maintenance mode.

This is my guest article at Inchoo website. My name is Alan Cole. Feel free to meet me at my website.

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


30 comments

  1. Ignored the line number reference (as I’m using Magento 1.9.x) and the line numbers aren’t the same.

    I put the code right above the maintenance portion. For me, it was line 64.

    Works like a charm and is exactly what I needed to access my Magento install while maintenance mode is active from a specific IP address without letting the public see it.

  2. Try This

    $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
    $maintenanceFile = 'maintenance.flag';
    // MAINTENANCE MODE: FIRST PLACE EMPTY FILE IN ROOT CALLED maintenance.flag
    // WHEN NOT IN MAINTENANCE MODE CHANGE maintenance.flag to maintenance.flag.txt
    // FOR MAINTENANCE MODE: UNCOMMENT THE FOLLOWING 2 LINES STARTING LINES WITH // (REPLACE COMMENTS TO TURN MODE OFF)
    $ip = $_SERVER['REMOTE_ADDR'];
    $allowed = array('71.66.117.15'); // IP ADDRESS ALLOWED TO VIEW THE SITE
    if (!file_exists($mageFilename)) {
        if (is_dir('downloader')) {
            header("Location: downloader");
        } else {
            echo $mageFilename." was not found";
        }
        exit;
    }
    // FOR MAGENTO MAINTENACE MODE: UNCOMMENT NEXT LINE BY REMOVING // AND THEN COMMENT LINE AFTER WITH // (REVERSE COMMENTS TO TURN MODE BACK ON)
    if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) {
    //if (file_exists($maintenanceFile)) {
        require( dirname(__FILE__) . '/errors/503.php');	
        exit;
    }
  3. Updated Code

    $mageFilename = MAGENTO_ROOT . ‘/app/Mage.php';
    $maintenanceFile = ‘maintenance.flag';
    // MAINTENANCE MODE: FIRST PLACE EMPTY FILE IN ROOT CALLED maintenance.flag
    // WHEN NOT IN MAINTENANCE MODE CHANGE maintenance.flag to maintenance.flag.txt
    // FOR MAINTENANCE MODE: UNCOMMENT THE FOLLOWING 2 LINES STARTING LINES WITH // (REPLACE COMMENTS TO TURN MODE OFF)
    $ip = $_SERVER['REMOTE_ADDR'];
    $allowed = array(‘ENTER YOU IP HERE”); // IP ADDRESS ALLOWED TO VIEW THE SITE
    if (!file_exists($mageFilename)) {
    if (is_dir(‘downloader’)) {
    header(“Location: downloader”);
    } else {
    echo $mageFilename.” was not found”;
    }
    exit;
    }
    // FOR MAGENTO MAINTENACE MODE: UNCOMMENT NEXT LINE BY REMOVING // AND THEN COMMENT LINE AFTER WITH // (REVERSE COMMENTS TO TURN MODE BACK ON)
    if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) {
    //if (file_exists($maintenanceFile)) {
        require( dirname(__FILE__) . ‘/errors/503.php’);
        exit;
    }

  4. $_SERVER['REMOTE_ATTR'] doesn’t work if your behind a reverse proxy or load balancer.
    You’ll also need to check $_SERVER["HTTP_X_FORWARDED_FOR"]

  5. Still no luck with this code in 1.7.0.2
    I copied exactly as written and get;

    PHP Parse error: syntax error, unexpected ‘;’, expecting T_PAAMAYIM_NEKUDOTAYIM on line 63

    any clue?

  6. Tried this in 1.7.0.2. Failed as per your instructions.

    $maintenanceFile = 'maintenance.flag';
    // MAINTENANCE MODE: FIRST PLACE EMPTY FILE IN ROOT CALLED maintenance.flag
    // WHEN NOT IN MAINTENANCE MODE CHANGE maintenance.flag to maintenance.flag.txt
    // FOR MAINTENANCE MODE: UNCOMMENT THE FOLLOWING 2 LINES STARTING LINES WITH // (REPLACE COMMENTS TO TURN MODE OFF)
    $ip = $_SERVER['REMOTE_ADDR'];
    $allowed = array('12.34.567.890'); // IP ADDRESS ALLOWED TO VIEW THE SITE
    if (!file_exists($mageFilename)) {
    	if (is_dir('downloader')) {
    	header("Location: downloader");    
    	} else {        
    		echo $mageFilename." was not found";
        	}
        	exit;
    }
    // FOR MAGENTO MAINTENACE MODE: UNCOMMENT NEXT LINE BY REMOVING // AND THEN COMMENT LINE AFTER WITH // (REVERSE COMMENTS TO TURN MODE BACK ON)
    if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) {
    //if (file_exists($maintenanceFile)) {
    	include_once dirname(__FILE__) . '/errors/503.php';
    exit;
    }

    You get completely blank pages on admin and frontend

  7. Revised: Here’s an updated easy version for maintenance mode workaround in Magento Version 1.7.0.2
    —————

    STARTING LINE 52 - CHANGE 
    
    $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
    $maintenanceFile = 'maintenance.flag';
    
    // MAINTENANCE MODE: FIRST PLACE EMPTY FILE IN ROOT CALLED maintenance.flag
    // WHEN NOT IN MAINTENANCE MODE CHANGE maintenance.flag to maintenance.flag.txt
    // FOR MAINTENANCE MODE: UNCOMMENT THE FOLLOWING 2 LINES STARTING LINES WITH // (REPLACE COMMENTS TO TURN MODE OFF)
    //$ip = $_SERVER['REMOTE_ADDR'];
    //$allowed = array('94.168.101.156'); // IP ADDRESS ALLOWED TO VIEW THE SITE
    
    if (!file_exists($mageFilename)) {
        if (is_dir('downloader')) {
    
            header("Location: downloader");
        } else {
            echo $mageFilename." was not found";
        }
        exit;
    }
    // FOR MAGENTO MAINTENACE MODE: UNCOMMENT NEXT LINE BY REMOVING // AND THEN COMMENT LINE AFTER WITH // (REVERSE COMMENTS TO TURN MODE BACK ON)
    //if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) {
    if (file_exists($maintenanceFile)) {
        include_once dirname(__FILE__) . '/errors/503.php';
        exit;
    }
  8. Hi,

    Is there any way to put only front side in maintenance and once user login to admin panel he can view the front?

    I am using magento community ver 1.7.0.0

    I have tired to search a lot but not found any solution for this.

  9. Surely it would be better to make this request the other way round? ie

    if (!in_array($ip, $allowed) && file_exists($maintenanceFile)) {

    or only load the IP resource if the file exists? ie

    if (file_exists($maintenanceFile) && !in_array($_SERVER['REMOTE_ADDR'], $allowed)) {
  10. The service is unavailable.

    message dispaly ..

    i did it some changes in \zend\cache\backend\file.php

    and create tmp folder in root dir. but its not solve .. please help any buddy…

    my magento site running perfectly but i will try to install magnento extentsion and install it. then after i click return to admin .. then i got this message…

    plz help me…

  11. change && in || and will work :)

    if (file_exists(‘maintenance.flag’)) {
    to
    if (file_exists(‘maintenance.flag’) || !in_array($ip, $allowed)) {

  12. This just doesn’t want to work for me… I’ve followed all the instructions and can’t seem to make it work.

    I’ve included my IP addresses, cleared all my caches, even tried to wrap the code in ” “, and yet I am still unable to access my site with the maintenance.flag file uploaded into my root folder.

    Does anyone else have any other solutions or suggestions on where I can find out the answer to this problem. I am running version 1.4.

    Thanks folks, this is a little above my skill level and is proving to be a daunting task trying to trouble shoot what is going wrong.

  13. If not work try to add the upper comas ” in the ($maintenanceFile)

    Ex:

    if (file_exists(‘maintenance.flag’)) {
    to
    if (file_exists(‘maintenance.flag’) && !in_array($ip, $allowed)) {

  14. Changing the directoryIndex isn’t a good idea. That will only stop people hitting your site with the base domain (www.mysite.com).

    Any deeper indexed or bookmarked url will either still work if it has /index.php/ in the url or will return a 404 if not.

    anyone hitting any bookmarked/indexed url which doesn’t explicitly contain

  15. If you are working behind the BIG-IP network, just to be sure this will not work. I tried many tricks but nothing happend. Then I found another simple solution but isn’t secure.

    Just change .httacces file of directory index parameter index.php to index.html then put a simple index.html page root of magento, thats it.

    After that, you can connect Magento with strong path, like put /index.php or /admin in your URL.

  16. I ran into the same problem as LZ did.
    But it was very easy to solve this issue:

    1. register for an account at dyndns.org (or some similar service) if you don’t have an account already
    2. if your router supports updating the dyndns.org service, configure your router for updating dyndns.org. Otherwise you have to install a client software which updates dyndns.org even if your ip has been changed
    3. Modify your index.php as described above by Alan
    4. Add the followig line (replace example.dyndns.org with your dynamic dns address)

    $myip = gethostbyname('example.dyndns.org');

    between

    $ip = $_SERVER['REMOTE_ADDR'];

    and

    $allowed = array('1.1.1.1','2.2.2.2'); // these are the IP's that are allowed to view the site.

    5. Modify the line

    if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) { 

    to

    if (file_exists($maintenanceFile) && !in_array($ip, $myip, $allowed)) { 

    6. That’s it!

    Now the index.php looks for the IP address which is assigned to your dynamic dns address (in this example example.dyndns.org). So you will always have access from your network when maintenance mode is enabled.

  17. Is it possible to put ip like

    11.22.33.* or
    11.22.*?

    Because I use wireless and my ip changes every time it drops! very annoying!

  18. Thanks for the article! Any way this can be used in a multi-store setup? Like preventing users to access domainA.com but letting them into domainB.com.

  19. Ok I found my problem – For this to work you will first need to enable Magentos Maintenance Mode

    Create a blank file and name it maintenance.flag and place the file into your root folder.

    The template for Maintenance Mode can be found: – errors/default/503.phtml

    Thank You

  20. Oddest problem. So your method has worked like a charm, except for the IP address I happen to be using at the moment (someone else’s home). Here, I get an error 503 while at every other IP I have allowed in, it has worked.

    This is what was listed as the description for the IP address here:
    IP Information: 72.171.0.144
    ISP: HUGHES NETWORK SYSTEMS
    Organization: HUGHES NETWORK SYSTEMS
    Connection: Broadband
    Proxy: Suspected Network Sharing Device
    City:
    Region:
    Country: United States united states flag

    Did you have any idea what could be the source of the problem? This website also seems to say that this computer is just outside of Wichita Kansas, when I am actually in northern Maryland. Perhaps this could cause some sort of problem?

    best,
    Dan

  21. Hi Ben,

    Glad you brought that up, because its something I tried. After some digging around this feature only restricts access to developer functions and not the site its self. (such as the log, or inline path hints)

    This allows only your IP to visit the site.

    Alan

  22. Hi Alan

    I’m pretty new to Magento, so this might be obvious, but how is this different to the ‘Allowed IPs’ section in Admin -> System -> Advanced -> Developer? Wouldn’t that do the same thing, but without the need to edit the index.php file or add in the file maintainance.flag?

    Of course, editing one file if you get it wrong is easier then editing the db, or wherever that information is held.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.