How to implement “Maintenance Mode” on already est

2019-01-13 14:16发布

I have built a website (PHP) with more than 60 pages. I have only now realized (unfortunately) that I should have built in an "In Maintenance Mode" feature to allow an admin to temporarily disable the website and point it to a Maintenance Mode page. This would only allow those logged in as an admin to view the website.

The options I see are:

  1. Add a new "include" file to the top of every single PHP page.

  2. I have one include that is used to display the navigation bar on
    every page (navigation class). I could write the Maintenance Mode
    code in this class.

Do I have any other options? The 1st option doesn't seem like the most efficient, and the 2nd one just seems like bad programming. Is there any other better way to include a new file on every single php file?

Thanks!

ps - the site is not launched yet

6条回答
不美不萌又怎样
2楼-- · 2019-01-13 14:41

You can use .htaccess to redirect to another page while on Maintenance Mode.

Three assorted examples:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]

.htaccess “Down For Maintenance” Page Redirect

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$ 
RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888

RewriteRule $ /maintenance.html [R=302,L] 

Redirect to maintenance page during upgrade using .htaccess

# redirect all visitors to alternate site but retain full access for you
ErrorDocument 403 http://www.alternate-site.com
Order deny,allow
Deny from all
Allow from 99.88.77.66

Maintenance mode for apache

查看更多
聊天终结者
3楼-- · 2019-01-13 14:50

I know this is an old question but I am adding this for future searchers who come across this page via searching.

I use a PHP redirect.

At the top of every page add the following lines:

<?php
include('PATH/TO/FILE/config.php');
if(maintenance == 1) {
header('Location: http://example.com/maintenance.php');
}
else {
// do nothing
}
?>

And in your config.php file just make a variable like so

$maintenance = 0; // turns maintenance mode off

When you want your site to be in maintenance mode just change the "0" to a "1". This will redirect all your site visitors to a maintenance page.

I typed that code pretty fast without testing it but it should work.

查看更多
爷、活的狠高调
4楼-- · 2019-01-13 14:52

I think, standard Apache expressions are more easily to understand than rewrite rules.

Before maintenance I rename file /home/coolcmd/site_maintenance_off to /home/coolcmd/site_maintenance_on.

Code snippet for /.htaccess:

# If file, directory or symlink /home/coolcmd/site_maintenance_on exists,
# and requested file is not /maintenance.html,
# and not an admin's IP...
<If "-e '/home/coolcmd/site_maintenance_on' && %{REQUEST_URI} != '/maintenance.html' && %{REMOTE_ADDR} != '111.111.111.111'">
# Generate status code 503 for URL beginning with /, in other words for ANY URL.
# This is not "the real redirection 3xx".
Redirect 503 "/"
# This page handles status code 503. All external resources used by page
# (for example, maintenance.css), if any, must be added to <if> above.
ErrorDocument 503 /maintenance.html
# Maintenance probably will end in 10 minutes.
Header set Retry-After 600
</If>
查看更多
贼婆χ
5楼-- · 2019-01-13 14:55

auto_prepend_file string

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require() function, so include_path is used.

The special value none disables auto-prepending.

you can set this in php.ini, or in apache (virtual) host file or .htaccess with php_flag auto_prepend_file file.php

[or php_admin_flag (?)]

edit

  • Maybe you should not put the include file in your web root dir or a sub folder.
  • And remember to call exit or die at the end.
查看更多
再贱就再见
6楼-- · 2019-01-13 14:57

Rather than specify IP Addresses, you could also check against HTTP_USER_AGENT in a RewriteCond..

RewriteCond %{HTTP_USER_AGENT} !MyCrazyString
RewriteCond %{THE_REQUEST} !upgrade\.txt
RewriteRule .* /upgrade.txt [R=307,L]

Anyone with "MyCrazyString" tagged onto the end of their Browser's User Agent string can get access, everyone else gets the maintenance mode page/site.

So long as high security isn't required, it's ideal; quick and easy to disseminate to admins, completely portable, and importantly, won't break if you reconnect your DSL when the only guy with actual server access is asleep at the other side of the world.

;o) Cor

查看更多
Ridiculous、
7楼-- · 2019-01-13 15:00

The simplest way would be to centralize some of the logic regarding site generation. This way you could turn maintenance mode on and redirect any non admins to a different page. This would not be hard to do, as I imagine you have some code that keeps popping up all over the place, so just extend the login functionality to check for a global variable (if you don't have any common global variables across your page you could just set it in .htaccess via setenv).

查看更多
登录 后发表回答