views:

411

answers:

4

I have built a fairly robust 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 bag 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  A: 

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

Maintenance mode for apache

Redirect to maintenance page during upgrade using .htaccess

Luis Melgratti
As you can see in the examples you can exclude some IP.
Luis Melgratti
That looks like a good quick alternative. I'm hoping to find other methods that may be more scalable so that each time I have a new admin, I don't have to update the .htaccess file and enter in their IP. Perhaps I'm getting a bit to ahead of myself though? For the next year I probably won't have very many new admin's. Perhaps implementing a simple quick .htaccess fix for now would work until I really need to implement the robust method. What do you guys think? Is it better to build it the "proper" way right now? Or just do the easy way until I need to make it more complex?
justinl
This requires you to know the IP addresses of all admins ...
OIS
Out of all of the options, I would say this is the best direction. It gives you the most flexibility and control site wide.
Dooltaz
Well this sounds like the best solution for the time being until I really need to build something complex considering this will take me only a few minutes to put together.
justinl
By the way, if anyone else uses this method, you will need to add a line into the .htaccess file to allow any external css files. To allow all css files use this:%{REQUEST_URI} !.css$
justinl
A: 

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

Miha Hribar
+3  A: 

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.
OIS
And you could add `exit;` somewhere at the end to stop the rest of your code from being parsed.
alex
@alex: yes. should not assume people know about exit just because they got 60+ pages of php. :I
OIS
haha yes thanks. It's easy to create lots of files and not really know what you're doing still :P
justinl
Thanks OIS for sharing about the auto_prepend_file. I didn't know about that and I'm confident that it will come in useful later. :)
justinl
A: 

Good tutorial, thanks for the help. greetings from germany