reenadak icon

md login protect

reenadak | PRO | 09/25/17 05:46:46 PM UTC | 0 ⭐ | 369 👁️ | Never ⏰ | []
PHP |

5.77 KB

|

None

|

0 👍

/

0 👎

<?php
// ====================== MD Login Protect v1.0 =============================
//
// Script Name          : MD Login Protect
// Script Version       : 1.0
// License              : Freeware
// Description          : Provide a login password access to any page ,by 
//                        including just one line. ( i.e. this file path )
//
// Inspired from        : http://www.zubrag.com/scripts/
// logout link          : <a href="http://site.com/page.php?logout">Logout</a>
// 
// Author               : Mukesh Dak
// Author_Email         : [email protected]
// Author_URL           : http://facebook.com/mukeshdak
//
// ===================================================================
 
// Login Username and Passwords array
$LOGIN_DB = array(
      'user' => 'password', 
      'user2' => 'password2'
    );
 
// request login? 
// true  -> show login and password boxes, 
// false -> show password box only
define('USE_USERNAME', true);
 
// User will be redirected to this page after logout
define('LOGOUT_URL', 'http://'.$_SERVER["HTTP_HOST"]);
 
// time out after this minutes of inactivity. Set to 0 to not timeout
define('TIMEOUT_MINUTES', 15);
 
// This parameter is only useful when TIMEOUT_MINUTES is not zero
// true  -> timeout time from last activity, 
// false -> timeout time from login
define('TIMEOUT_CHECK_ACTIVITY', true);
 
if(isset($_GET['help']))
    {
      die('Include following code into every page you would like to protect,
      at the very beginning (first line):<br><?php include("
      ' . str_replace('\\','\\\\',__FILE__) . '"); ?>');
    }
 
// ##################### DO NOT EDIT SCRIPT BELOW THIS LINE ##################
 
// timeout in seconds
$timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);
 
// logout?
if(isset($_GET['logout'])) {
  setcookie("verify", '', $timeout, '/'); // clear password;
  header('Location: ' . LOGOUT_URL);
  exit();
}
 
if(!function_exists('show_login_form'))
    {
    // show login form
    function show_login_form($error_msg)
    {
    
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Login Required</title>
      <meta http-equiv="Content-Type"   content="text/html; charset=UTF-8" />
      <meta http-equiv="CACHE-CONTROL"  content="NO-CACHE">
      <meta http-equiv="PRAGMA"         content="NO-CACHE">
 
      <style type="text/css" media="all" charset=UTF-8 >
        body                { padding:0px; margin:0px; color:#222; font-size:12px; font-family:"Lucida Grande", Arial; }
        label               { width:30%; float:left; text-align:right; padding:2px; }
 
        #titlebox           { height:60px; width:350px; text-align:center; font-size:36px; font-weight:bold; color:#F60; line-height:60px; margin-top:100px; }
        #titlebox a         { color:#CCC; text-decoration:none; }
        #titlebox a:hover   { color:#F60; text-decoration:none; }
 
        #loginbox           { background:#fec; width:250px; padding:50px; height:90px; border:1px #F60 solid; color:#f60; -moz-border-radius:10px; }
 
        input               { color:#C00; padding:2px; border:#F60 1px solid; background-color:#fff; display:block; }
        input:hover         { cursor:pointer; color:#111; border:#363 1px dotted;}
 
        .loginwrap          { width:350px; margin-left:auto; margin-right:auto; text-align:center; height:100%; }
 
        .loginfooter        { width:350px; height:50px; line-height:50px; text-align:center; color:#F60; }
        .loginfooter a      { color:#F30; }
 
        .error              { color: #f00;  margin-bottom: 15px; display:block;}
 
        .form_field         { margin-bottom: 15px;  }
      </style>
    </head>
 
    <body>
        <div class="loginwrap">
            <div id="titlebox">Login Required</div>
                <form id="login" method="post">
                    <div id="loginbox">
 
                        <div class="error">
                            <?php echo $error_msg; ?>
                        </div>
 
                        <?php if (USE_USERNAME) { ?>
                            <div class="form_field">
                                <label>Login : </label>
                                <input type="input" name="access_login" />
                            </div>  
                        <?php } ?>
 
                            <div class="form_field">
                                <label>Password : </label>
                                <input type="password" name="access_password" />
                            </div>
 
                            <div class="form_field">
                                <label></label> 
                                <input type="submit" name="Submit" value="Login" />
                            </div>
                    </div>
                </form>
 
                <div class="loginfooter">           
                    Powered by <a href="http://facebook.com/mukeshdak" >MD Login Protect v1.0</a>
                </div>
            </div>
        </div>
    </body>
    </html>
 
    <?php die(); // stop at this point
    }
    }
 
 
// user provided password
if (isset($_POST['access_password']))
    {
      $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
      
      $pass = $_POST['access_password'];
 
      if (!USE_USERNAME && !in_array($pass, $LOGIN_DB) || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_DB) || $LOGIN_DB[$login] != $pass ) ) )
        {
            show_login_form("Incorrect password.");
        }
        else
        {
            // set cookie if password was validated
            setcookie("verify", md5($login.'%'.$pass), $timeout, '/');
        
            // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
            // So need to clear password protector variables
            unset($_POST['access_login']);
            unset($_POST['access_password']);
            unset($_POST['Submit']);
        }
    }
 
else
    {
      // check if password cookie is set
      if (!isset($_COOKIE['verify']))
        {
            show_login_form("");
        }
 
      // check if cookie is good
      $found = false;
 
      foreach($LOGIN_DB as $key=>$val) 
        {
            // creating cookie variable for each login.
            $lp = (USE_USERNAME ? $key : '') .'%'.$val;
 
            if ($_COOKIE['verify'] == md5($lp))
            {
                $found = true;
 
                // prolong timeout
                if (TIMEOUT_CHECK_ACTIVITY)
                {
                    setcookie("verify", md5($lp), $timeout, '/');
                }
                
                break;
            }
      }
 
      if (!$found) // Cookie is not as per usernames and passwords.
        {
            show_login_form("");
        }
    }
?>

Comments