0

I found this post about how to create a "down for maintenance" page but I'm having some trouble getting it to work properly.

define('MAINTENANCE', 1); 
if(MAINTENANCE > 0){
    require('maintenance.php'); die(); 
}

When I place this code in /webroot.index.php it works. However, the answer suggests adding an IP address check so that if it's down, I would still be able to view it and make sure any updates went through smoothly.

So, it would look something like this

define('MAINTENANCE', 0); 
if(MAINTENANCE > 0 && $_SERVER['REMOTE_ADDR'] !='188.YOUR.IP.HERE'){
    require('maintenance.php'); die(); 
}

The issue is, my IP address will NOT be detected by cake. I've typed echo $_SERVER['REMOTE_ADDR'] and it just shows ::1. I also tried using my user_id but I got the following error Class 'AuthComponent' not found in....

I tried putting this in /index.php and /App/index.php but the maintenance page wasn't triggered and the page loads normally.

Community
  • 1
  • 1
user1852176
  • 455
  • 1
  • 9
  • 29
  • `::1` is localhost in ipv6. Is this running on your own computer? – Cfreak Oct 20 '13 at 03:59
  • 1
    Use a `307` redirect to a static page, and then show a static page using a `503` status code. This tells search engines the redirect is temporary and the server is in maintenance. – Reactgular Oct 20 '13 at 04:01
  • @Cfreak yes I'm running on localhost. If anything, wouldn't it show the IP assigned by my router? So does this mean when I upload it to a live server it would show my "real" Ip address? – user1852176 Oct 20 '13 at 04:07
  • @MathewFoscarini where would I place this? Can you give an example? – user1852176 Oct 20 '13 at 04:08
  • If you are looking for a simple Cake-only version of it, see [this](http://www.dereuromark.de/2013/09/29/moving-a-cakephp-app/) - it describes how you can use a CakePHP shell to activcate and deactivate maintenance mode - including a whitelisting of IPs. – mark Oct 20 '13 at 23:33
  • 1
    No it would not show the IP assigned by your router. A service running on your local machine should run even if it wasn't connected to anything at all because all computers have a local loopback networking interface. When you upload to a server on the internet it would show the IP of your router on the internet (assuming your router uses NAT which is probably 99% of home networks) – Cfreak Oct 21 '13 at 00:23

1 Answers1

1

What I normally do is using mod_rewrite, that way it's independent of my application code.

Here's an example, it redirects all access attempts that do not origin from 127.0.0.1 to maintenance.php in the same folder as the .htaccess file:

RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteRule . maintenance.php [L]

The maintenance.php could then look something like this:

<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Retry-After: 86400');

?><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Temporarily Unavailable</title>
</head><body>
<h1>Service Temporarily Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
</body></html>

Note that this doesn't invole an external redirect! I never had any problems with search engines yet, but maybe this was just because the maintenance never took very long, not sure, I'm no SEO expert.

Using an actual redirect could be done using the Redirect flag (and a non-match pattern to avoid a redirect loop):

RewriteRule !^maintenance\.php$ /maintenance.php [R=307,L]

This would redirect to /maintenance.php in case the URL isn't already pointing there.

To make this all even less tied to the application, you could define appropriate rules in the server or virtual host config instead (in case you have access to that), this is what I would prefer, as it's safe to override all application code that way.

ndm
  • 59,784
  • 9
  • 71
  • 110