Advertisement
reenadak

md login protect

Sep 25th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.77 KB | None | 0 0
  1. <?php
  2. // ====================== MD Login Protect v1.0 =============================
  3. //
  4. // Script Name          : MD Login Protect
  5. // Script Version       : 1.0
  6. // License              : Freeware
  7. // Description          : Provide a login password access to any page ,by
  8. //                        including just one line. ( i.e. this file path )
  9. //
  10. // Inspired from        : http://www.zubrag.com/scripts/
  11. // logout link          : <a href="http://site.com/page.php?logout">Logout</a>
  12. //
  13. // Author               : Mukesh Dak
  14. // Author_Email         : md@mukeshdak.com
  15. // Author_URL           : http://facebook.com/mukeshdak
  16. //
  17. // ===================================================================
  18.  
  19. // Login Username and Passwords array
  20. $LOGIN_DB = array(
  21.       'user' => 'password',
  22.       'user2' => 'password2'
  23.     );
  24.  
  25. // request login?
  26. // true  -> show login and password boxes,
  27. // false -> show password box only
  28. define('USE_USERNAME', true);
  29.  
  30. // User will be redirected to this page after logout
  31. define('LOGOUT_URL', 'http://'.$_SERVER["HTTP_HOST"]);
  32.  
  33. // time out after this minutes of inactivity. Set to 0 to not timeout
  34. define('TIMEOUT_MINUTES', 15);
  35.  
  36. // This parameter is only useful when TIMEOUT_MINUTES is not zero
  37. // true  -> timeout time from last activity,
  38. // false -> timeout time from login
  39. define('TIMEOUT_CHECK_ACTIVITY', true);
  40.  
  41. if(isset($_GET['help']))
  42.     {
  43.       die('Include following code into every page you would like to protect,
  44.       at the very beginning (first line):<br><?php include("
  45.       ' . str_replace('\\','\\\\',__FILE__) . '"); ?>');
  46.     }
  47.  
  48. // ##################### DO NOT EDIT SCRIPT BELOW THIS LINE ##################
  49.  
  50. // timeout in seconds
  51. $timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);
  52.  
  53. // logout?
  54. if(isset($_GET['logout'])) {
  55.   setcookie("verify", '', $timeout, '/'); // clear password;
  56.   header('Location: ' . LOGOUT_URL);
  57.   exit();
  58. }
  59.  
  60. if(!function_exists('show_login_form'))
  61.     {
  62.     // show login form
  63.     function show_login_form($error_msg)
  64.     {
  65.    
  66.     ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  67.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  68.     <html xmlns="http://www.w3.org/1999/xhtml">
  69.     <head>
  70.       <title>Login Required</title>
  71.       <meta http-equiv="Content-Type"   content="text/html; charset=UTF-8" />
  72.       <meta http-equiv="CACHE-CONTROL"  content="NO-CACHE">
  73.       <meta http-equiv="PRAGMA"         content="NO-CACHE">
  74.  
  75.       <style type="text/css" media="all" charset=UTF-8 >
  76.         body                { padding:0px; margin:0px; color:#222; font-size:12px; font-family:"Lucida Grande", Arial; }
  77.         label               { width:30%; float:left; text-align:right; padding:2px; }
  78.  
  79.         #titlebox           { height:60px; width:350px; text-align:center; font-size:36px; font-weight:bold; color:#F60; line-height:60px; margin-top:100px; }
  80.         #titlebox a         { color:#CCC; text-decoration:none; }
  81.         #titlebox a:hover   { color:#F60; text-decoration:none; }
  82.  
  83.         #loginbox           { background:#fec; width:250px; padding:50px; height:90px; border:1px #F60 solid; color:#f60; -moz-border-radius:10px; }
  84.  
  85.         input               { color:#C00; padding:2px; border:#F60 1px solid; background-color:#fff; display:block; }
  86.         input:hover         { cursor:pointer; color:#111; border:#363 1px dotted;}
  87.  
  88.         .loginwrap          { width:350px; margin-left:auto; margin-right:auto; text-align:center; height:100%; }
  89.  
  90.         .loginfooter        { width:350px; height:50px; line-height:50px; text-align:center; color:#F60; }
  91.         .loginfooter a      { color:#F30; }
  92.  
  93.         .error              { color: #f00;  margin-bottom: 15px; display:block;}
  94.  
  95.         .form_field         { margin-bottom: 15px;  }
  96.       </style>
  97.     </head>
  98.  
  99.     <body>
  100.         <div class="loginwrap">
  101.             <div id="titlebox">Login Required</div>
  102.                 <form id="login" method="post">
  103.                     <div id="loginbox">
  104.  
  105.                         <div class="error">
  106.                             <?php echo $error_msg; ?>
  107.                         </div>
  108.  
  109.                         <?php if (USE_USERNAME) { ?>
  110.                             <div class="form_field">
  111.                                 <label>Login : </label>
  112.                                 <input type="input" name="access_login" />
  113.                             </div> 
  114.                         <?php } ?>
  115.  
  116.                             <div class="form_field">
  117.                                 <label>Password : </label>
  118.                                 <input type="password" name="access_password" />
  119.                             </div>
  120.  
  121.                             <div class="form_field">
  122.                                 <label></label>
  123.                                 <input type="submit" name="Submit" value="Login" />
  124.                             </div>
  125.                     </div>
  126.                 </form>
  127.  
  128.                 <div class="loginfooter">          
  129.                     Powered by <a href="http://facebook.com/mukeshdak" >MD Login Protect v1.0</a>
  130.                 </div>
  131.             </div>
  132.         </div>
  133.     </body>
  134.     </html>
  135.  
  136.     <?php die(); // stop at this point
  137.     }
  138.     }
  139.  
  140.  
  141. // user provided password
  142. if (isset($_POST['access_password']))
  143.     {
  144.       $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
  145.      
  146.       $pass = $_POST['access_password'];
  147.  
  148.       if (!USE_USERNAME && !in_array($pass, $LOGIN_DB) || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_DB) || $LOGIN_DB[$login] != $pass ) ) )
  149.         {
  150.             show_login_form("Incorrect password.");
  151.         }
  152.         else
  153.         {
  154.             // set cookie if password was validated
  155.             setcookie("verify", md5($login.'%'.$pass), $timeout, '/');
  156.        
  157.             // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
  158.             // So need to clear password protector variables
  159.             unset($_POST['access_login']);
  160.             unset($_POST['access_password']);
  161.             unset($_POST['Submit']);
  162.         }
  163.     }
  164.  
  165. else
  166.     {
  167.       // check if password cookie is set
  168.       if (!isset($_COOKIE['verify']))
  169.         {
  170.             show_login_form("");
  171.         }
  172.  
  173.       // check if cookie is good
  174.       $found = false;
  175.  
  176.       foreach($LOGIN_DB as $key=>$val)
  177.         {
  178.             // creating cookie variable for each login.
  179.             $lp = (USE_USERNAME ? $key : '') .'%'.$val;
  180.  
  181.             if ($_COOKIE['verify'] == md5($lp))
  182.             {
  183.                 $found = true;
  184.  
  185.                 // prolong timeout
  186.                 if (TIMEOUT_CHECK_ACTIVITY)
  187.                 {
  188.                     setcookie("verify", md5($lp), $timeout, '/');
  189.                 }
  190.                
  191.                 break;
  192.             }
  193.       }
  194.  
  195.       if (!$found) // Cookie is not as per usernames and passwords.
  196.         {
  197.             show_login_form("");
  198.         }
  199.     }
  200. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement