Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Cache-Control: no-cache, must-revalidate');
- ?>
- <?php
- session_name('login');
- session_start();
- // handle forms
- if(isset($_POST['action'])) {
- // login form
- if($_POST['action'] == 'login') {
- if(isset($_POST['username']) && isset($_POST['password'])) {
- if(($hash = sha1($_POST['password'].'magicsalt'.$_POST['username']))
- == 'e48695647d8ec2db8447b83e2782a078ace97a5c') {
- $_SESSION['user'] = array('username' => $_POST['username'], 'hash' => $hash);
- }
- }
- }
- // log out form
- if($_POST['action'] == 'logout') {
- session_unset();
- }
- // add cookie form
- if($_POST['action'] == 'cookie') {
- if(isset($_POST['key']) && isset($_POST['value']) && isset($_POST['age'])) {
- setcookie($_POST['key'], $_POST['value'], time()+$_POST['age'], '/sudoku.php', 'localhost');
- header('Refresh: 0;');
- exit();
- }
- print('<br><br>');
- foreach ($_COOKIE as $key => $value) {
- print($key.'<br>');
- print_r(' '.$_COOKIE[$key]);
- print('<br><br>');
- }
- }
- }
- // display content
- if(isset($_SESSION['user'])) {
- print_r($_SESSION['user']);
- print('
- <form method="POST">
- <input type="text" name="key" placeholder="Key"><br>
- <input type="text" name="value" placeholder="Value"><br>
- <input type="number" name="age" placeholder="Age"><br>
- <input type="hidden" name="action" value="cookie">
- <input type="submit" value="Create"><br>
- </form>
- ');
- foreach($_COOKIE as $key => $value) {
- print($key.'<br>');
- print(' '.$value.'<br>');
- }
- print('
- <form method="POST" style="position: absolute; right: 0; bottom: 0; margin: 0; padding: 5px;">
- <input type="hidden" name="action" value="logout">
- <input type="submit" value="Log Out">
- </form>
- ');
- }else {
- print('
- <form method="POST">
- <input type="text" name="username" placeholder="Username"><br>
- <input type="password" name="password" placeholder="Password"><br>
- <input type="hidden" name="action" value="login">
- <input type="submit" value="Login"><br>
- </form>
- ');
- }
- $counter = $_COOKIE['counter'] ?? 0;
- $counter += 1;
- setcookie('counter', $counter, time()+60*60*24*365);
- print('<span style="position: absolute; bottom: 0; left: 0;"> user counter: '.$counter.'</span>');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement