Advertisement
b7334957

Protect Page the best

Dec 15th, 2023
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.93 KB | Source Code | 0 0
  1. <?php
  2. ###############################################################
  3. # Page Password Protect 2.13
  4. ###############################################################
  5. # Visit http://www.zubrag.com/scripts/ for updates
  6. ###############################################################
  7. #
  8. # Usage:
  9. # Set usernames / passwords below between SETTINGS START and SETTINGS END.
  10. # Open it in browser with "help" parameter to get the code
  11. # to add to all files being protected.
  12. #       Example: password_protect.php?help
  13. # Include protection string which it gave you into every file that needs to be protected
  14. #
  15. # Add following HTML code to your page where you want to have logout link
  16. # <a href="http://www.example.com/path/to/protected/page.php?logout=1">Logout</a>
  17. #
  18. ###############################################################
  19. /*
  20. -------------------------------------------------------------------
  21. SAMPLE if you only want to request login and password on login form.
  22. Each row represents different user.
  23. $LOGIN_INFORMATION = array(
  24.     'zubrag' => 'root',
  25.     'test' => 'testpass',
  26.     'admin' => 'passwd'
  27. );
  28. --------------------------------------------------------------------
  29. SAMPLE if you only want to request only password on login form.
  30. Note: only passwords are listed
  31. $LOGIN_INFORMATION = array(
  32.     'root',
  33.     'testpass',
  34.     'passwd'
  35. );
  36. --------------------------------------------------------------------
  37. */
  38. ##################################################################
  39. #   SETTINGS START
  40. ##################################################################
  41. // Add login/password pairs below, like described above
  42. // NOTE: all rows except last must have comma "," at the end of line
  43. $LOGIN_INFORMATION = array(
  44.     'reinhold',
  45.     'habekeinpasswort',
  46.     'keinpasswort',
  47. );
  48. // request login? true - show login and password boxes, false - password box only
  49. define('USE_USERNAME', false);
  50. // User will be redirected to this page after logout
  51. define('LOGOUT_URL', './');
  52. // time out after NN minutes of inactivity. Set to 0 to not timeout
  53. define('TIMEOUT_MINUTES', 20);
  54. // This parameter is only useful when TIMEOUT_MINUTES is not zero
  55. // true - timeout time from last activity, false - timeout time from login
  56. define('TIMEOUT_CHECK_ACTIVITY', true);
  57. ##################################################################
  58. #   SETTINGS END
  59. ##################################################################
  60. ///////////////////////////////////////////////////////
  61. // do not change code below
  62. ///////////////////////////////////////////////////////
  63. // show usage example
  64. if(isset($_GET['help'])) {
  65.     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;');
  66. }
  67. // timeout in seconds
  68. $timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);
  69. // logout?
  70. if(isset($_GET['logout'])) {
  71.     setcookie("verify", '', $timeout, '/'); // clear password;
  72.     header('Location: ' . LOGOUT_URL);
  73.     exit();
  74. }
  75. if(!function_exists('showLoginPasswordProtect')) {
  76. // show login form
  77. function showLoginPasswordProtect($error_msg) {
  78. ?>
  79. <html>
  80. <head>
  81. <title>PDEVDES // PLAYGROUND</title>
  82. <link rel="apple-touch-icon" sizes="180x180" href="favicon/apple-touch-icon.png">
  83. <link rel="icon" type="image/png" sizes="32x32" href="favicon/favicon-32x32.png">
  84. <link rel="icon" type="image/png" sizes="16x16" href="favicon/favicon-16x16.png">
  85. <meta http-equiv="cache-control" content="no-cache">
  86. <meta http-equiv="pragma" content="no-cache">
  87. <link href="css/font-face/roboto-regular/roboto-regular.css" rel="stylesheet">
  88. <link href="css/animate.min.css" rel="stylesheet">
  89. <link href="css/meyerreset.css" rel="stylesheet">
  90. <link href="css/normalize.css" rel="stylesheet">
  91. <link href="css/style.css" rel="stylesheet">
  92. <style>
  93. *, *:before, *:after {
  94.     box-sizing:border-box;
  95.     margin:0;
  96. }
  97.  
  98. html {
  99.    
  100. }
  101. body {
  102.     background:#282a36;
  103.     color:#d3d7cf;
  104.     font-family:'Roboto', sans-serif;
  105.     font-size:1em;
  106. }
  107. #login {
  108.     border-radius: 8px;
  109.     background: #999;
  110.     border: 1px solid #111;
  111.     color: #111;
  112.     margin: 20% auto auto;
  113.     max-width: 500px;
  114.     padding: 10px
  115. }
  116. #errormsg,input {
  117.     padding: 10px;
  118.     width: 100%
  119. }
  120. #errormsg,input,label {
  121.     color: #fff;
  122.     display: block
  123. }
  124. #errormsg {
  125.     background: #c00;
  126.     position: absolute;
  127.     text-align: center;
  128.     top: 0
  129. }
  130. input,label {
  131.     margin: 0 0 5px
  132. }
  133. input {
  134.     border: 1px solid #111;
  135.     color: #2e3436
  136. }
  137. input[type=input],input[type=password] {
  138.     background: #c1c1c1;
  139.     transition: .5s
  140. }
  141. input[type=input]:focus,input[type=password]:focus {
  142.     background: #dcdcdc
  143. }
  144. input[type=submit] {
  145.     background: #6e6e6e;
  146.     color: #eee;
  147.     display: block;
  148.     margin: 0 0 5px;
  149.     width: 100%;
  150.     transition: .5s
  151. }
  152. input[type=submit]:hover {
  153.     background: #00bb25;
  154.     color: #fff
  155. }
  156. ::-webkit-input-placeholder {
  157.     color: #555753
  158. }
  159. :-ms-input-placeholder {
  160.     color: #555753
  161. }
  162. ::placeholder {
  163.     color: #555753
  164. }
  165. h3 {
  166.     margin: 0 0 10px
  167. }
  168. </style>
  169. </head>
  170. <body>
  171. <?php echo $error_msg; ?>
  172. <div id="login">
  173.     <h3>DEVDES // PLAYGROUND</h3>
  174.     <form method="post">
  175.         <?php if (USE_USERNAME) echo '
  176.         <input type="input" name="access_login" placeholder="Username" onfocus="this.placeholder=\'\'" onblur="this.placeholder=\'Username\'" autocomplete="off"/>';?>
  177.         <input type="password" name="access_password" placeholder="Password" onfocus="this.placeholder=''" onblur="this.placeholder='Password'" autocomplete="off"/>
  178.         <input type="submit" name="Submit" value="LOGIN" />
  179.     </form>
  180. </div>
  181. </body>
  182. </html>
  183. <?php
  184.     // stop at this point
  185.     die();
  186. }
  187. }
  188. // user provided password
  189. if (isset($_POST['access_password'])) {
  190.     $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
  191.     $pass = $_POST['access_password'];
  192.     if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)
  193.     || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) )
  194.     ) {
  195.         showLoginPasswordProtect('<div id="errormsg" class="animated infinite flash delay-2s">Incorrect password.</div>');
  196.     }
  197.     else {
  198.         // set cookie if password was validated
  199.         setcookie("verify", md5($login.'%'.$pass), $timeout, '/'); // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
  200.         // So need to clear password protector variables
  201.         unset($_POST['access_login']);
  202.         unset($_POST['access_password']);
  203.         unset($_POST['Submit']);
  204.     }
  205. }
  206. else {
  207.     // check if password cookie is set
  208.     if (!isset($_COOKIE['verify'])) {
  209.         showLoginPasswordProtect("");
  210.     }
  211.     // check if cookie is good
  212.     $found = false;
  213.     foreach($LOGIN_INFORMATION as $key=>$val) {
  214.         $lp = (USE_USERNAME ? $key : '') .'%'.$val;
  215.         if ($_COOKIE['verify'] == md5($lp)) {
  216.             $found = true;
  217.             // prolong timeout
  218.             if (TIMEOUT_CHECK_ACTIVITY) {
  219.                 setcookie("verify", md5($lp), $timeout, '/');
  220.             }
  221.             break;
  222.         }
  223.     }
  224.     if (!$found) {
  225.         showLoginPasswordProtect("");
  226.     }
  227. }
  228. ?>
Tags: protect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement