Advertisement
Guest User

Untitled

a guest
Sep 7th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. <?php
  2. session_start();
  3. class TOKEN {
  4. public static function generate() {
  5. return $_SESSION['token'] = base64_encode(openssl_random_pseudo_bytes(15));
  6. }
  7. public static function check($token) {
  8. if (isset($_SESSION['token']) && $token === $_SESSION['token']) {
  9. unset($_SESSION['token']);
  10. return true;
  11. }
  12. return false;
  13. }
  14. }
  15. ?>
  16. <?php
  17. $display_form = FALSE;
  18. if (isset($_POST['submit'])) {
  19. $username = $_POST['username'];
  20. $userpass = $_POST['userpass'];
  21.  
  22. if (strlen($username) < 4) {
  23. $error_name = 'required';
  24. $display_form = true;
  25. $validation_error = true;
  26. }
  27. if (strlen($userpass) < 8) {
  28. $error_pass = 'required';
  29. $display_form = true;
  30. $validation_error = true;
  31. }
  32. if (!$validation_error) {
  33. if (TOKEN::check($_POST['token'])) {
  34. echo 'process form';
  35. } else {
  36. echo 'invalid security token';
  37. }
  38. }
  39. } else {
  40. $display_form = TRUE;
  41. }
  42. ?>
  43. <!DOCTYPE html>
  44. <html lang="en">
  45. <head>
  46. <meta charset="UTF-8">
  47. <title>Title</title>
  48. </head>
  49. <body>
  50. <?php
  51. if ($display_form == true) {
  52. ?>
  53. <form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>">
  54. <input type="hidden" name="token" value="<?php echo TOKEN::generate(); ?>">
  55. <input type="text" name="username" id="" placeholder="username">
  56. <?php echo $error_name; ?>
  57. <br>
  58. <input type="password" name="userpass" id="" placeholder="Password">
  59. <?php echo $error_pass; ?>
  60. <br>
  61. <input type="submit" name="submit" value="Sign in">
  62. </form>
  63. </body>
  64. </html>
  65. <?php
  66. }
  67. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement