12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- date_default_timezone_set("Europe/Kiev");
-
- $credentials = array("login" => "admin",
- "password" => "qwerty");
- session_start();
- $nothingInPost = true;
- $passwordInPostRight = false;
- $login = "";
- if (isset($_GET['logout']) and $_GET['logout'] == 'true'):
- $_SESSION['logged'] = false;
- endif;
- if (!isset($_SESSION['logged'])):
- $_SESSION['logged'] = $passwordInPostRight;
- endif;
- if (isset($_POST['login']) && isset($_POST['password'])):
- $nothingInPost = false;
- $login = $_POST['login'];
- if (($_POST['login'] == $credentials['login']) && ($_POST['password'] == $credentials['password'])):
- $passwordInPostRight = true;
- $_SESSION['loginTimes'][] = time();
- endif;
- $_SESSION['logged'] = $passwordInPostRight;
- endif;
- ?>
- <html>
- <head>
- <title>
- php template hell sample
- </title>
- </head>
- <body>
- <?php if (!$_SESSION['logged']): ?> <!-- Если пользователь не залогинен -->
- <?php if (!$passwordInPostRight && !$nothingInPost):
- <div style='color: red;'>
- Your password are wrong!!!
- </div>
- <?php endif; ?>
- <form method='post' action="">
- Login: <input type='text' name='login' value="<?php echo $login; //сюда мы засовываем login из POST или пустую строку ?>"><br/>
- Password: <input type='password' name='password'><br/>
- <input type='submit' value='Login' />
- </form>
- <?php else:
- <?php if ($passwordInPostRight):
- <div style='color: green;'> Welcome </div>
- <?php endif; ?>
- История логинов:
- <table border='1'>
- <?php foreach ($_SESSION['loginTimes'] as $time): ?>
- <tr><td> <?php echo date("Y-m-d H:i:s", $time); ?> </td></tr>
- <?php endforeach; ?>
- </table>
- <a href='?logout=true'>Выйти</a>
- <?php endif; ?>
- <pre><?php print_r(array("nothingInPost" => $nothingInPost, "passwordInPostRight" => $passwordInPostRight, "login" => $login));
- print_r($_SESSION);
- print_r($_POST);
- print_r($_GET);
-
- ?>
- </pre>
- </body>
- </html>
-
|