Guest User

Easy Password Protected Page with Persistent Login

a guest
Oct 22nd, 2012
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. <?php
  2.     // The master password.
  3.     $password = "pass";
  4.  
  5.     // Used to get the filename
  6.     $u = explode('/', $_SERVER["SCRIPT_NAME"]);
  7.     $script = $u[count($u) - 1];
  8.  
  9.     // Used to tell if the user is logged in and whether or not they wish to logout.
  10.     session_start();
  11.     if(isset($_POST["password"]) && ($_POST["password"] == $password)) $_SESSION["authed"] = true;
  12.     if(isset($_GET["action"]) && ($_GET["action"] == "logout")) { $_SESSION["authed"] = false; header("Location: " . $script); }
  13.     if(!$_SESSION["authed"]):
  14. ?>
  15. <!DOCTYPE html>
  16. <html>
  17.     <head>
  18.         <title>Login</title>
  19.         <style type="text/css">
  20.             *{margin:0;padding:0}html,body{height:100%;background-color:#eaeaea;font-family:Tahoma,Geneva,sans-serif;color:#333}input{border:1px solid #aaa;border-radius:4px;padding:4px;color:#aaa;width:192px}input:hover{border:1px solid #777;color:#333}input:focus{color:#333;outline:0;border:1px solid #5096e1;-webkit-box-shadow:0 0 4px rgba(80,150,225,0.5);-moz-box-shadow:0 0 4px rgba(80,150,225,0.5);box-shadow:0 0 4px rgba(80,150,225,0.5)}input[type="submit"]{width:200px;background-color:#fff}input[type="submit"]:active{-webkit-box-shadow:0 0 6px rgba(50,50,50,0.35);-moz-box-shadow:0 0 6px rgba(50,50,50,0.35);box-shadow:0 0 6px rgba(50,50,50,0.35)}.wrapper{height:100%;display:table;margin:0 auto;vertical-align:middle}.contentwrapper{display:table-cell;vertical-align:middle}.content{position:relative;border:1px solid #aaa;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px;background-color:#fff;padding:20px;margin-top:14px;-webkit-box-shadow:0 0 20px rgba(50,50,50,0.35);-moz-box-shadow:0 0 20px rgba(50,50,50,0.35);box-shadow:0 0 20px rgba(50,50,50,0.35)}.header{position:absolute;width:96px;height:20px;top:-15px;left:50%;padding:4px;margin-left:-51px;text-align:center;background-color:#fff;border:1px solid #aaa;-webkit-border-radius:32px;-moz-border-radius:32px;border-radius:32px -webkit-box-shadow:0 0 2px rgba(50,50,50,0.35);-moz-box-shadow:0 0 2px rgba(50,50,50,0.35);box-shadow:0 0 2px rgba(50,50,50,0.35)}.section{margin-top:8px;text-align:center}.error,.success{margin-top:0;color:red;font-size:13px;max-width:200px}.success{color:green}
  21.         </style>
  22.  
  23.         <!--[if lt IE 9]>
  24.             <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  25.         <![endif]-->
  26.     </head>
  27.     <body onload="document.getElementById('password').focus()">
  28.         <div class="wrapper">
  29.             <div class="contentwrapper">
  30.                 <div class="content">
  31.                     <div class="header">Login</div>
  32.                     <form action="<?=$script?>" method="post">
  33.                         <div class="section"><input type="password" name="password" id="password" placeholder="Password"></div>
  34.                         <div class="section"><input type="submit" value="Login"></div>
  35.                     </form>
  36.                 </div>
  37.             </div>
  38.         </div>
  39.     </body>
  40. </html>
  41. <?php else: ?>
  42.  
  43. <!-- Your hidden content -->
  44. <!DOCTYPE html>
  45. <html>
  46.     <head>
  47.         <title>Title</title>
  48.     </head>
  49.     <body>
  50.         <p>Content!</p>
  51.  
  52.         <!-- If you want to let the user logout, simply direct them to '?action=logout' with $script like so: -->
  53.         <a href="<?=$script?>?action=logout"><input type="button" value="Logout"></a>
  54.     </body>
  55. </html>
  56. <!-- End of your hidden content -->
  57.  
  58. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment