Advertisement
Guest User

Untitled

a guest
Aug 8th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. <?php
  2. # AUTHENTICATION CONTROLLER:
  3. error_reporting(E_ALL);
  4. session_start();
  5.  
  6. # require config:
  7. require_once $_SERVER['DOCUMENT_ROOT'] . "shoppingCART/lib/constants.inc.php";
  8.  
  9. # get the database:
  10. require DATABASE;
  11.  
  12. # get auth library:
  13. require AUTH_LIB;
  14.  
  15. # get output lib:
  16. require_once OUTPUT;
  17.  
  18. # variables required:
  19. $form_token = hash('sha512', rand() . md5(rand() ) ); # required when submitting the login form.
  20. $ip_address = $_SERVER['REMOTE_ADDR'];
  21. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  22. $failed_attempts = array();
  23. $max_failed_attempts = 5;
  24. $error = array(); # array to collect errors
  25.  
  26. # set the sessions:
  27. if (!isset($_SESSION['form_token']) && !isset($_SESSION['form_token_start']) )
  28. {
  29. $_SESSION['form_token'] = $form_token;
  30. $_SESSION['form_token_start'] = time();
  31. }
  32.  
  33.  
  34.  
  35.  
  36. # calculate token age:
  37. $token_age = time() - $_SESSION['form_token_start'];
  38.  
  39. # check if session has expired:
  40. if ($token_age < 900)
  41. {
  42. # check if correct submit button was clicked and that everything was filled in:
  43. if (isset($_POST['username']) && isset($_POST['password'])
  44. && isset($_POST['action']) && $_POST['action'] == "Sign In"
  45. && $_SESSION['form_token'] === $_POST['form_token']
  46. )
  47. {
  48. if (!empty($_POST['username']) && !empty($_POST['password']))
  49. {
  50. # make user input safe:
  51. $username = userInput($_POST['username']);
  52. $password = userInput($_POST['password']);
  53.  
  54. # check if valid username and password:
  55. if (validUser($username, $password) )
  56. {
  57. # get the users url token from the datbase:
  58. $user = getUserDetails($username);
  59. foreach ($user as $user_value)
  60. {
  61. $url_token = $user_value['url_token'];
  62. }
  63.  
  64. # the following token will now be used in each request the user makes:
  65. $user_token = hash('sha256', rand() . $user_agent . $ip_address . $url_token);
  66.  
  67. setcookie("user_token", "$user_token-$ip_address", time()+4500);
  68.  
  69. if (!isset($_SESSION['user_token']) )
  70. {
  71. $_SESSION['user_token'] = $user_token;
  72. }
  73.  
  74. echo "<a href='?token=$user_token'>Click here</a>";
  75.  
  76. echo "<p>" . $username; # REDIRECT TO USER HOME HERE, show content here
  77. die();
  78. }
  79. else
  80. {
  81. # FAILED login attempt:
  82. $failed_attempts[] = 1;
  83. $error['invalid'][] = "Username or Password is invalid.";
  84. $_SESSION['form_token'] = $form_token; # generate new form token
  85. }
  86. }
  87. else
  88. {
  89. # empty username or password
  90. $failed_attempts[] = 1;
  91. $error['empty'][] = "Enter a Username and Password.";
  92. $_SESSION['form_token'] = $form_token; # generate new form token
  93. }
  94. }
  95. }
  96. else
  97. {
  98. $error['timeout'] = "Session Expired.";
  99. }
  100.  
  101. # after user has been validated, check that this is the correct cookie:
  102.  
  103.  
  104. if (isset($_COOKIE['user_token']) && $_SESSION['user_token'])
  105. {
  106.  
  107. if (isset($_GET['token']))
  108. {
  109. $cookie = explode("-", $_COOKIE['user_token']);
  110. if ($cookie[0] === $_GET['token'] && $cookie[1] === $_SERVER['REMOTE_ADDR']) # match ip address
  111. {
  112. $session_token = $_SESSION['user_token'];
  113. $ip_address = $_SERVER['REMOTE_ADDR'];
  114. setcookie("user_token", "$session_token-$ip_address", time()+4500 ); #allowed to continue, otherwise show form
  115. }
  116. }
  117. }
  118. else
  119. {
  120. require h_LOGIN; # show html form
  121. die();
  122. }
  123.  
  124. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement