Guest User

Untitled

a guest
Mar 17th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2. function before_output () {
  3.     $html = ob_get_contents();
  4.     return $html;
  5. }
  6.  
  7. ob_start('before_output');
  8.  
  9. if (session_id() == '') session_start();
  10. ?>
  11. <!DOCTYPE html>
  12. <html lang="es" dir="ltr">
  13.     <head>
  14.         <meta charset="UTF-8" />
  15.         <title>Location</title>
  16.         <style type="text/css" media="screen">
  17.             * {margin:0;padding:0;}
  18.             body {font-family:"Segoe UI";}
  19.             p, h1, pre, ul {margin-bottom:15px;}
  20.             .wrapper {width:960px;margin:0 auto;}
  21.             input, button {padding:5px;}
  22.             .error {background-color:#eb0000;list-style:none;padding: 15px;}
  23.             .error li {color:#fff;}
  24.             .success {background-color:#59b804; color: #fff;padding: 10px;}
  25.         </style>
  26.     </head>
  27.     <body>
  28.         <div class="wrapper">
  29.             <h1>Login Form</h1>
  30.             <?php if (isset($_SESSION['errors'])): ?>
  31.                 <ul class="error">
  32.                     <?php foreach ($_SESSION['errors'] as $error): ?>
  33.                     <li><?php echo $error; ?></li>
  34.                     <?php endforeach; ?>
  35.                 </ul>
  36.                 <?php unset($_SESSION['errors']); ?>
  37.             <?php endif; ?>
  38.             <?php if (isset($_SESSION['user-logged']) && $_SESSION['user-logged'] === true): ?>
  39.                 <p class="success">You are logged in now.</p>
  40.             <?php else: ?>
  41.             <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" autocomplete="off">
  42.                 <p><input type="text" name="Login[username]" value="" /></p>
  43.                 <p><input type="password" name="Login[password]" value="" /></p>
  44.                 <button name="Login[submit]" type="submit" value="true">Log In</button>
  45.             </form>        
  46.             <?php endif; ?>
  47.             <?php
  48.                 if ($_SERVER['REQUEST_METHOD'] === 'POST' && array_key_exists('Login', $_POST)) {
  49.                     $username = (isset($_POST['Login']['username']))
  50.                         ? htmlspecialchars(trim($_POST['Login']['username']), ENT_QUOTES, 'UTF-8')
  51.                         : '';
  52.                     $password = (isset($_POST['Login']['password']))
  53.                         ? htmlspecialchars(trim($_POST['Login']['password']), ENT_QUOTES, 'UTF-8')
  54.                         : '';
  55.  
  56.                     if ($username !== 'admin') {
  57.                         $_SESSION['errors'][] = 'Nombre de usuario, no existe.';
  58.                     }
  59.                     if ($password !== 'secret') {
  60.                         $_SESSION['errors'][] = 'Contraseña incorrecta, vuelva a intentarlo';
  61.                     }
  62.                     if ($username === 'admin' && $password === 'secret') {
  63.                         $_SESSION['user-logged'] = true;
  64.                     }
  65.  
  66.                     header("Location: {$_SERVER['SCRIPT_NAME']}");
  67.                 }
  68.             ?>
  69.         </div>
  70.     </body>
  71. </html>
Add Comment
Please, Sign In to add comment