Guest User

Untitled

a guest
Aug 27th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <?php
  2.  
  3. if(!(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))){
  4. header('WWW-Authenticate: Basic realm="Restricted Area');
  5. header('HTTP/1.0 401 Unauthorized');
  6. die('Acesso Não Autorizado!');
  7. }
  8.  
  9. $validPasswords = ["neto" => "1234"];//consulta ao banco para login e guardar em array ['login' = > 'Senha'];
  10. $validUser = array_keys($validPasswords);
  11. //recebe usuário e senha do cliente
  12. $user = $_SERVER['PHP_AUTH_USER'];
  13. $pass = $_SERVER['PHP_AUTH_PW'];
  14.  
  15. $validate = (in_array($user, $validUser) && $pass = $validPasswords[$user]);
  16.  
  17. if (!$validate){
  18. header('WWW-Authenticate: Basic realm="Restricted Area');
  19. header('HTTP/1.0 401 Unauthorized');
  20. die('Acesso Não Autorizado!');
  21. }
  22.  
  23. echo "ENTROU";
  24. ?>
  25.  
  26. <!DOCTYPE html>
  27. <html>
  28. <head>
  29. <title>Autenticação HTTP</title>
  30. <meta charset="ISO-8859-1">
  31. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  32. <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
  33. </head>
  34. <body>
  35. <form name="cookieform" id="login" method="get">
  36. <input type="text" name="username" id="username" />
  37. <input type="password" name="password" id="password" />
  38. <input type="submit" name="sub" value="Submit" onclick="auth()" />
  39. </form>
  40.  
  41. <script>
  42. var username = $("#username").val();
  43. var password = $("#password").val();
  44.  
  45. function auth(){
  46. $.ajax({
  47. type: "GET",
  48. url: "login.php",
  49. dataType: 'json',
  50. async: false,
  51. data: '{"username": "' + username + '", "password" : "' + password + '"}',
  52. beforeSend: function (xhr) {
  53. xhr.setRequestHeader ("Authorization", "Basic " + btoa(function (user, password) {
  54. var tok = user + ':' + password;
  55. var hash = Base64.encode(tok);
  56. return "Basic " + hash;
  57. }));
  58. },
  59. success: function (response){
  60. alert(response);
  61. },
  62. error: function (response){
  63. alert(response);
  64. }
  65. });
  66. }
  67. </script>
  68. </body>
  69. </html>
  70.  
  71. function auth(){
  72. // using XMLHttpRequest
  73. var username = $("input#username").val();
  74. var password = $("input#password").val();
  75. var xhr = new XMLHttpRequest();
  76. xhr.open("GET", "login.php", true);
  77. xhr.withCredentials = true;
  78. xhr.setRequestHeader("Authorization", btoa(function (username, password) {
  79. var tok = user + ':' + password;
  80. var hash = Base64.encode(tok);
  81. return 'Basic ' + hash;
  82. }));
  83. xhr.onload = function () {
  84. console.log(xhr.responseText);
  85. };
  86. xhr.send();
  87. }
Add Comment
Please, Sign In to add comment