Advertisement
AtomMota

Login Script

Mar 18th, 2013
2,804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.48 KB | None | 0 0
  1. <?php
  2.  
  3. ###############################################################
  4. # Page Password Protect 2.13
  5. ###############################################################
  6. # Visit http://www.zubrag.com/scripts/ for updates
  7. ###############################################################
  8. #
  9. # Usage:
  10. # Set usernames / passwords below between SETTINGS START and SETTINGS END.
  11. # Open it in browser with "help" parameter to get the code
  12. # to add to all files being protected.
  13. #    Example: password_protect.php?help
  14. # Include protection string which it gave you into every file that needs to be protected
  15. #
  16. # Add following HTML code to your page where you want to have logout link
  17. # <a href="http://www.example.com/path/to/protected/page.php?logout=1">Logout</a>
  18. #
  19. ###############################################################
  20.  
  21. /*
  22. -------------------------------------------------------------------
  23. SAMPLE if you only want to request login and password on login form.
  24. Each row represents different user.
  25.  
  26. $LOGIN_INFORMATION = array(
  27.   'zubrag' => 'root',
  28.   'test' => 'testpass',
  29.   'admin' => 'passwd'
  30. );
  31.  
  32. --------------------------------------------------------------------
  33. SAMPLE if you only want to request only password on login form.
  34. Note: only passwords are listed
  35.  
  36. $LOGIN_INFORMATION = array(
  37.   'root',
  38.   'testpass',
  39.   'passwd'
  40. );
  41.  
  42. --------------------------------------------------------------------
  43. */
  44.  
  45. ##################################################################
  46. #  SETTINGS START
  47. ##################################################################
  48.  
  49. // Add login/password pairs below, like described above
  50. // NOTE: all rows except last must have comma "," at the end of line
  51. $LOGIN_INFORMATION = array(
  52.   'a' => 'a',
  53.   'b' => 'b'
  54. );
  55.  
  56. // request login? true - show login and password boxes, false - password box only
  57. define('USE_USERNAME', true);
  58.  
  59. // User will be redirected to this page after logout
  60. define('LOGOUT_URL', 'http://www.example.com/');
  61.  
  62. // time out after NN minutes of inactivity. Set to 0 to not timeout
  63. define('TIMEOUT_MINUTES', 0);
  64.  
  65. // This parameter is only useful when TIMEOUT_MINUTES is not zero
  66. // true - timeout time from last activity, false - timeout time from login
  67. define('TIMEOUT_CHECK_ACTIVITY', true);
  68.  
  69. ##################################################################
  70. #  SETTINGS END
  71. ##################################################################
  72.  
  73.  
  74. ///////////////////////////////////////////////////////
  75. // do not change code below
  76. ///////////////////////////////////////////////////////
  77.  
  78. // show usage example
  79. if(isset($_GET['help'])) {
  80.   die('Include following code into every page you would like to protect, at the very beginning (first line):<br>&lt;?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?&gt;');
  81. }
  82.  
  83. // timeout in seconds
  84. $timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);
  85.  
  86. // logout?
  87. if(isset($_GET['logout'])) {
  88.   setcookie("verify", '', $timeout, '/'); // clear password;
  89.   header('Location: ' . LOGOUT_URL);
  90.   exit();
  91. }
  92.  
  93. if(!function_exists('showLoginPasswordProtect')) {
  94.  
  95. // show login form
  96. function showLoginPasswordProtect($error_msg) {
  97. ?>
  98. <html>
  99. <head>
  100.   <title>Please enter password to access this page</title>
  101.   <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
  102.   <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
  103. </head>
  104. <body>
  105.   <style>
  106.     input { border: 1px solid black; }
  107.   </style>
  108.   <div style="width:500px; margin-left:auto; margin-right:auto; text-align:center">
  109.   <form method="post">
  110.     <h3>Please enter password to access this page</h3>
  111.     <font color="red"><?php echo $error_msg; ?></font><br />
  112. <?php if (USE_USERNAME) echo 'Login:<br /><input type="input" name="access_login" /><br />Password:<br />'; ?>
  113.     <input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" />
  114.   </form>
  115.   <br />
  116.   <a style="font-size:9px; color: #B0B0B0; font-family: Verdana, Arial;" href="http://www.zubrag.com/scripts/password-protect.php" title="Download Password Protector">Powered by Password Protect</a>
  117.   </div>
  118. </body>
  119. </html>
  120.  
  121. <?php
  122.   // stop at this point
  123.   die();
  124. }
  125. }
  126.  
  127. // user provided password
  128. if (isset($_POST['access_password'])) {
  129.  
  130.   $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
  131.   $pass = $_POST['access_password'];
  132.   if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)
  133.   || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) )
  134.   ) {
  135.     showLoginPasswordProtect("Incorrect password.");
  136.   }
  137.   else {
  138.     // set cookie if password was validated
  139.     setcookie("verify", md5($login.'%'.$pass), $timeout, '/');
  140.    
  141.     // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
  142.     // So need to clear password protector variables
  143.     unset($_POST['access_login']);
  144.     unset($_POST['access_password']);
  145.     unset($_POST['Submit']);
  146.   }
  147.  
  148. }
  149.  
  150. else {
  151.  
  152.   // check if password cookie is set
  153.   if (!isset($_COOKIE['verify'])) {
  154.     showLoginPasswordProtect("");
  155.   }
  156.  
  157.   // check if cookie is good
  158.   $found = false;
  159.   foreach($LOGIN_INFORMATION as $key=>$val) {
  160.     $lp = (USE_USERNAME ? $key : '') .'%'.$val;
  161.     if ($_COOKIE['verify'] == md5($lp)) {
  162.       $found = true;
  163.       // prolong timeout
  164.       if (TIMEOUT_CHECK_ACTIVITY) {
  165.         setcookie("verify", md5($lp), $timeout, '/');
  166.       }
  167.       break;
  168.     }
  169.   }
  170.   if (!$found) {
  171.     showLoginPasswordProtect("");
  172.   }
  173.  
  174. }
  175.  
  176. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement