19 Kudos
Don't
move!

There are a few ways to do this, probably the most common way is to put your site in maintenance mode and edit maintenance-page.tpl.php. This method works well and can produce the desired results, however, it’s not the proper way of doing things. What if you still want to have the use of maintenance mode for it’s intended purposes. Well this could still work, but you’re going to have to fiddle with the permissions each time. Also the maintenance mode message will appear on your site, so that will have to be removed and if you want it back, you’re going to have to edit code.

Here is another way, without having to fiddle around with any backend code.

  • Make sure PHP filter is enabled in the modules
  • Create a new block
  • Enter your template information in there, here is an example based on a bootstrap design

<div class="container">
  <div class="row">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Access Denied - Please Login or Register</h3>
      </div>
      <div class="panel-body">
        <div class="row">
          <div class="col-sm-6">
            <h3> Login </h3>
            <hr>
            <?php 
              $loginForm = drupal_get_form('user_login');
              print drupal_render($loginForm); 
            ?>
          </div>
          <div class="col-sm-6">
            <h3> Register </h3>
            <hr>
            <?php 
              $registerForm = drupal_get_form('user_register_form');
              print drupal_render($registerForm); 
            ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
  • Set the text format to “PHP”
  • Go to Visibility Settings > Pages > Pages on which this PHP code returns TRUE
  • Enter this code:

<?php
  global $user;
  $url = $_SERVER['REQUEST_URI'];
  if ($user->uid || strpos($url,'user')) {
    return false;
  }
  else {
    return true;
  }
?>
  • Save block
  • Place the block into the content region and save the changes.
  • Go to permissions and change the Node permission – View published content for anonymous users. Uncheck the box.

Done.