How can I turn off my site temporarily ?

cerbere

New Member
Messages
51
Reaction score
1
Points
0
When doing maintenance or modifications to my site,
I'd like to lock it up for a short period (1-4 hours), maybe
with a page that says "Back at 16:00 h GMT" and
allows me to enter a password so I can do my testing.

This is easily done in the public_html/index.htm page,
but how can I lock the whole site without having to edit
every page ?

In other words, I want to flip a SINGLE switch that allows
only me to access any site page.

Any suggestions ?
 

Sohail

Active Member
Messages
3,055
Reaction score
0
Points
36
Well using the options in the cPanel you can password-protect directories.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Check out .htaccess and .htpasswd files that can control access.

You only have to change one file (.htaccess)
 

dickey

New Member
Messages
128
Reaction score
0
Points
0
Can you give example of how to do that using .htaccess as I wanted to ask the same question. :)
 

cerbere

New Member
Messages
51
Reaction score
1
Points
0
Yes freecrm, can you please give some details on how
to lock the site using .htaccess ? Is that configuration
file used by Apache ?
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Yes freecrm, can you please give some details on how
to lock the site using .htaccess ? Is that configuration
file used by Apache ?

I'll post a bit later and cover this - I'm just doing a bit of complex dev work that needs my attention....
 

cerbere

New Member
Messages
51
Reaction score
1
Points
0
After some research on Google with "Apache" and
"htaccess" as keywords, I tried to use cPanel to enforce
user/password access to my site. When I impose that
on my public_html directory, here is what happens :

- First access after firing up my browser : I get the
user/password pop-up window; after entering the user
and pass I specified to cPanel, I get an "Internal server
error" page.

- Subsequent accesses (i.e. hitting "refresh") : no more
user/password dialog, only the "Internal server error"
page.

I FEEL I am close to doing what I want (see my opening
message), but I am still doing something wrong... :pigeon:(
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
It's more of a hack than a fix, but assuming you have an include(header.php) or similar throughout your site, you could add in a line to the header that reads an entry in the database (SELECT active FROM website) and
Code:
if(active == false) //show Site Closed

It's just a thought until someone finds the proper fix.
 

dickey

New Member
Messages
128
Reaction score
0
Points
0
It's more of a hack than a fix, but assuming you have an include(header.php) or similar throughout your site, you could add in a line to the header that reads an entry in the database (SELECT active FROM website) and
Code:
if(active == false) //show Site Closed
It's just a thought until someone finds the proper fix.

I like this idea, maybe create an alternative page. then check a dbase value like for website_status and if logged in as admin, then if in maintennance mode display the alternate page, then maybe you can use a password to unlock the page for you to make you logged in as admin, so while the rest of the world see the maintennance mode page, you can easily test your site. :)
nice idea man. I'll try that.
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
If you're going to run with that idea, you don't even need it to be just true/false. If you used numbers you could set explanation pages.

E.g.
0 = all fine, page loads as expected
1 = Scheduled maintenance
2 = Unscheduled maintenance
3 = In development / Under construction
etc. etc.

You could add extra details too, such as when the site is expected to come back.

This'd let you take your site down simply by changing a couple of items in your database and restore it just as quickly.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
OK - back -

Two ways you can do this..

If you just want to completely turn off the site, all you need is an .htaccess file saved in your site root.

Content would simply be

Code:
order deny,allow
allow from [your IP address]
deny from all

This allows you to access your own site (provided your IP is static) but no-one else.

Alternatively, you can force a login:

Two files

.htaccess

Code:
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /path/.htpasswd
AuthGroupFile /dev/null

<Limit GET POST>

require valid-user

</Limit>

.passwd

This simply contains a list of username:encryptedpassword.

Check out http://www.4webhelp.net/us/password.php to create them

Hope this helps...
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
I have a sneaky feeling that's not what is being gone for.

Have you tried to get on a broswer game whilst it's been down for maintenance? Torn City and Kingdom of Loathing both do it - the whole site shuts down for about 30 minutes a night to do all the server processes. Anyone trying to play is sent to a "Game down for maintenance" page, regardless of where they came from.

TBH - if you accessed a website and suddenly a password prompt appeared or an access denied, I'd be more concerned they'd been hacked than think it was maintenance.
 

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
Well what you can do i guess (its what i used for my site) is have a textbox that you can put special "Commands" in like "openSite9784" and "closeSite7389"

And then you can place in the code for it to place into a textfile

PHP:
<?php

$command = $_POST['command'];
$openString = "openSite9784"; //String for the open command
$closeString = "closeSite7389"; //String for the close command
$file = "status.txt"; //File location
$fo = fopen($file, 'w') or die("Error opening status.txt");

// 0 = CLOSED
// 1 = OPEN

	if($command != NULL){
		if($command == $openString){
			fwrite($fo, "1");
			fclose($fo);
		}
		elseif($command == $closeString){
			fwrite($fo, "0");
			fclose($fo);
		}
		else{
			echo "Invalid command";
		}
	}
	else{}

?>

And now in the main page we check if it is "1 for open" or "0 for closed"

PHP:
<?php

$file = "status.txt";
$fo = fopen($file, 'r') or die("Error opening status.txt");
$fread = fread($fo, filesize($file));

	if($fread == "0"){
		echo "This website is closed temporarily";
	}
	elseif($fread == "1"){
?>

<html>
<head>
</head>
<body>

HTML

</body>
</html>

<?php
	}
	else{
		echo "Error";
	}
?>

This should all be working but i just made this off the top of my head.


EDIT:

Tested it, works perfectly.
If it helped please rep me :D
 
Last edited:

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
Useful Code and a Useful Link I later found

you dont really need .htpasswd

In your .htaccess file, to redirect your whole site:
Code:
Redirect 302 / http://www.yoursite.com/redirectpage.html

To redirect just a page, use something like:
Code:
Redirect 302 /oldpage.html http://www.yoursite.com/newpage.html

The 302 code stands for a temporary redirect. If you're going to FTP, make sure to send it as ascii and not binary. Also, you might already have an .htaccess redirect for 404 pages, you can just append the code above to it, to suit your needs. For single page redirects, the .htaccess needs to be in that page's directory. If you're doing the whole site, you need to put .htaccess in the public_html directory.

One other useful code is to redirect non html pages to the root directory: Like yoursite.com/index.php to yoursite.com
Code:
Options +FollowSymLinks
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3, 9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=302,L]

Again, 302 is temporary redirects. 302 should be use for permanent redirects. This should makes a difference to search engines.


_____________________________________________

This following link should have exactly what you need. It is specifically for sites under construction. http://www.askapache.com/htaccess/htaccess-for-webmasters.html#under-construction-code




.
 
Last edited:

dickey

New Member
Messages
128
Reaction score
0
Points
0
Then I'd go for the PHP hack or fix, since it gives me more control. There shouldn't be panic caused to your visitors. :)
This is something I recently worked out. hope someone can give me feedback.
PHP:
<?php
  session_start();                                                          //if main page has a session_start() omit this line but 
                                                                            //include file after the session start.
  $mysqli = mysqli_connect('localhost','username','password','site_stat');  //open the database for connections
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();                                                                 //if cannot connect generate what error happened
  }
                                                                            //commands to enable disable the site.
  if (isset($_SESSION['admin'])) $logged = $_SESSION['admin'];              //checks if user site admin is logged in
  
  if (isset($_GET['set_mode_on'])) {                                        //command to enter maintennance mode 
    if ($logged==1)                                                         //(e.g. http://www.sitename.com?set_mode_on)
    {
      $qstring="update site_mode set normal=0";
      $result = mysqli_query($mysqli,$qstring);
    }
  }
  if (isset($_GET['set_mode_off'])) {                                       //command to enter normal mode
    if ($logged==1) 							    //(e.g. http://www.sitename.com?set_mode_off)
    {
      $qstring="update site_mode set normal=1";
      $result = mysqli_query($mysqli,$qstring);
    }
  }
                                                                            //command to login
                                                                            //www.sitename.com?login
  if (isset($_GET['login'])) 					            //to prevent people from accidentally finding this login
  {                                                                         //I actually used a weird sentence without space like
	                                                                    //thequickbrownfoxjumpsoverthelazydog
    if ($_POST)								    //If there is post data process it.
    {
      $pass=$_POST['uname'];
      $qstring = "select password from site_mode";
      $result = mysqli_query($mysqli,$qstring);
      $row = mysqli_fetch_array($result);
      mysqli_free_result($result);

      if ($row['password']==$pass) 
      {
        $_SESSION['admin'] = 1;						    // if password correct store true to admin var in session
        $temp = "
          <form name='modeswitch' method='POST' action='".$_SERVER['PHP_SELF']."?set_mode_on'>
            <input type='submit' value='Maintennance mode' />               
          </form>
          <form name='modeswitch' method='POST' action='".$_SERVER['PHP_SELF']."?set_mode_off'>
            <input type='submit' value='Normal mode' />
          </form>";                                                         //simple button interface but may be removed
        echo $temp; 
        mysqli_close($mysqli);
        exit();
      }
    } else {
      $_SESSION['admin']=0;						    //any login attempts automatically logs you out (Security reasons)
      $temp = "
        <form name='login' method='POST' action='".$_SERVER['PHP_SELF']."?login'>
          <table>
            <tr> 
              <td>Username</td>
              <td>:</td>
              <td><input type='password' name='uname' /></td>
            </tr>
            <tr><td>
              <input type='submit' Value='Submit' /></td>
            </tr>
          </table>
        </form>";
      echo $temp;						            //login form and bypass.
      exit();
    }
  }
                                                                            //if no command was issued via get...
  if (!$logged)                                                             //check if logged in if not...
  {
    $qstring = "Select normal from site_mode";                              //check if site is in maintennance mode
    $result = mysqli_query($mysqli, $qstring);
    $row = mysqli_fetch_array($result);
    mysqli_free_result($result);
    $normal = $row['normal'];
  
    if ($normal==0) {                                                      //if in maintennance mode include the warning page
      include "warning.php";                                               // the warning page is a small message
      exit();
    }
  } 									   //there is no else because this script should do nothing if logged in
									   //or not in maintennance mode.
  mysqli_close($mysqli);
?>
this is the main code.
I also created a mysql database called site_stat,
it contains a table named 'site_mode'...
site mode currently has 2 fields
normal as int
and
password as varchar

normal will contain a value 0 if in maintennance mode...
PHP:
<?php
  $html_body.="
    <div class='main_contents'>
        <h2>The site \"http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."\" is temporarily unavailable</h2>
        <p>It is currently being updated, tested and is under maintennance mode please check back again later</p>
    </div>
    <div class='footer'>
      <script type='text/javascript' src='http://www.ubuntu.com/files/countdown/display.js'></script>
    </div>";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>Dickey Stuff</title>
    <script type='text/javascript' src='jokes.js'></script>
    <link rel='stylesheet' type='text/css' href='css.css' />
  </head>
  <body>
    <div class='dokyument'>
      <?php print $html_body; ?>
    </div>
  </body>
</html>
and this is the warning.php

basically to use this I just make it the first include of every page...
it suits my needs as even my basic html page is made in php form see warning.php
 
Last edited:
Top