Advertisement
reenadak

password protect script

Sep 25th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. <?php
  2. # Page Password Protect 2.13
  3. # Visit http://www.zubrag.com/scripts/ for updates
  4. # logout link : <a href="http://www.example.com/path/to/protected/page.php?logout=1">Logout</a>
  5.  
  6. // Add login/password pairs below, like described above
  7. // NOTE: all rows except last must have comma "," at the end of line
  8. $LOGIN_INFORMATION = array(
  9.   'ilove' => 'password',
  10.   'rna' => 'password'
  11. );
  12.  
  13. // request login? true - show login and password boxes, false - password box only
  14. define('USE_USERNAME', true);
  15.  
  16. // User will be redirected to this page after logout
  17. define('LOGOUT_URL', 'http://mukeshdak.com/');
  18.  
  19. // time out after NN minutes of inactivity. Set to 0 to not timeout
  20. define('TIMEOUT_MINUTES', 60);
  21.  
  22. // This parameter is only useful when TIMEOUT_MINUTES is not zero
  23. // true - timeout time from last activity, false - timeout time from login
  24. define('TIMEOUT_CHECK_ACTIVITY', true);
  25.  
  26.  
  27. /* I does not wish people to see location of this script of mine, so I commented this out
  28. if(isset($_GET['help'])) {
  29.   die('Include following code into every page you would like to protect, at the very beginning (first line):<br><?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?>');
  30. } */
  31.  
  32. // timeout in seconds
  33. $timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);
  34.  
  35. // logout?
  36. if(isset($_GET['logout'])) {
  37.   setcookie("verify", '', $timeout, '/'); // clear password;
  38.   header('Location: ' . LOGOUT_URL);
  39.   exit();
  40. }
  41.  
  42. if(!function_exists('showLoginPasswordProtect')) {
  43.  
  44. // show login form
  45. function showLoginPasswordProtect($error_msg) {
  46. ?>
  47. <html>
  48. <head>
  49.   <title>Please enter password to access this page</title>
  50.   <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
  51.   <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
  52. </head>
  53. <body>
  54.   <style>
  55.     input { border: 1px solid black; }
  56.   </style>
  57.   <div style="width:500px; margin-left:auto; margin-right:auto; text-align:center">
  58.   <form method="post">
  59.     <h3>Please enter password to access this page</h3>
  60.     <font color="red"><?php echo $error_msg; ?></font><br />
  61. <?php if (USE_USERNAME) echo 'Login:<br /><input type="input" name="access_login" /><br />Password:<br />'; ?>
  62.     <input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" />
  63.   </form>
  64.   <br />
  65.   <a style="font-size:9px; color: #B0B0B0; font-family: Verdana, Arial;" href="http://facebook.mukeshdak.com" title="Contact Mukesh Dak to get this script.">Password Protection Script v1.0 by MD</a>
  66.   </div>
  67. </body>
  68. </html>
  69.  
  70. <?php
  71.   // stop at this point
  72.   die();
  73. }
  74. }
  75.  
  76. // user provided password
  77. if (isset($_POST['access_password'])) {
  78.  
  79.   $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
  80.   $pass = $_POST['access_password'];
  81.   if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)
  82.   || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) )
  83.   ) {
  84.     showLoginPasswordProtect("Incorrect password.");
  85.   }
  86.   else {
  87.     // set cookie if password was validated
  88.     setcookie("verify", md5($login.'%'.$pass), $timeout, '/');
  89.    
  90.     // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
  91.     // So need to clear password protector variables
  92.     unset($_POST['access_login']);
  93.     unset($_POST['access_password']);
  94.     unset($_POST['Submit']);
  95.   }
  96.  
  97. }
  98.  
  99. else {
  100.  
  101.   // check if password cookie is set
  102.   if (!isset($_COOKIE['verify'])) {
  103.     showLoginPasswordProtect("");
  104.   }
  105.  
  106.   // check if cookie is good
  107.   $found = false;
  108.   foreach($LOGIN_INFORMATION as $key=>$val) {
  109.     $lp = (USE_USERNAME ? $key : '') .'%'.$val;
  110.     if ($_COOKIE['verify'] == md5($lp)) {
  111.       $found = true;
  112.       // prolong timeout
  113.       if (TIMEOUT_CHECK_ACTIVITY) {
  114.         setcookie("verify", md5($lp), $timeout, '/');
  115.       }
  116.       break;
  117.     }
  118.   }
  119.   if (!$found) {
  120.     showLoginPasswordProtect("");
  121.   }
  122.  
  123. }
  124.  
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement