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.

18
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Discussion 18 Comments

Add Comment
  1. Hello Alan,

    Congratulations on your first guest article on our site. I hope we’ll see them more in the future. Btw. Great site you have.

  2. Ben

    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.

  3. 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

  4. Khan

    Fantastic this will defo come in handy! Thanks!

  5. Dan

    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

  6. Mike

    1.4.1.1 – not working .. :(

  7. Mike

    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

  8. Max

    Thanks!

  9. Mario

    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.

  10. LZ

    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!

  11. Frank

    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.

  12. O?uz Çelikdemir

    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.

  13. Steve

    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

  14. Rod

    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)) {

  15. joanna

    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.

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

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

  17. 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…

  18. 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)) {
    

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top