Standalone PHP Maintenance Mode Access Protection

September 5, 2012 | PHP | 1 comment

maintenanceJust a quickie from me today. It seems that htaccess htpasswd protection is not as pretty as some would like. This is ok because 2mins of PHP work and we have this:

<?php
   if (!$_COOKIE['has_access']) {
     if (isset($_POST['get_access'])) {
         if ($_POST['password'] == 'password') {
            setcookie('has_access', 1, (time() + (86400*30)));

             echo 'You\'re in! Click <a href="http://www.yoursite.com">here</a> to see visit the site..';
             die;
         }
    }

    echo '<h1>Site under construction. Please login here or try back later.</h1>';
    echo ' <form method="POST">
    Password: <input type="password" name="password" value="" />
    <input type="submit" name="get_access" value="Login" />
    </form>';
    die;
}
?>

It will check for the existence of a cookie.. this is more unsecure than htpasswd however sometimes you just want a basic holding page whilst a site is under development or some temporary access protection.

The cookie will last for 30 days and the default password is password.

Use the following code at the top of any file you want to password protect:

<?php require(‘check_access.php’); ?>

Simple enough.. just add some basic styling and a logo and you’re good to go. Serves a purpose and is a stage better than htpasswd.

Enjoy!

A Donate Button!

1 Comment

  1. sarabjeet

    I really like that you are giving information on PHP MYSql .Being enrolled on a website development course i found your information very helpful indeed. Thanks for it.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

CommentLuv badge

Divi United

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!