Daniel3996

login.php

Dec 23rd, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. <?php
  2.  
  3. if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])){
  4. if (isset($_POST['username']) && isset($_POST['password'])) {
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7.  
  8. require __DIR__."/conn.php";
  9.  
  10. // Prepare a query to retrieve user information based on the provided username
  11. $query = "SELECT * FROM `User` WHERE username = ?";
  12. $stmt = mysqli_prepare($conn, $query);
  13. mysqli_stmt_bind_param($stmt, 's', $username);
  14. mysqli_stmt_execute($stmt);
  15. $result = mysqli_stmt_get_result($stmt);
  16.  
  17. if (mysqli_num_rows($result) == 1) {
  18. $user = mysqli_fetch_assoc($result);
  19.  
  20. $recaptcha_secret = "//secret_key";
  21. $recaptcha_response = $_POST['g-recaptcha-response'];
  22.  
  23. $recaptcha_url = "https://www.google.com/recaptcha/api/siteverify";
  24. $recaptcha_data = [
  25. 'secret' => $recaptcha_secret,
  26. 'response' => $recaptcha_response,
  27. ];
  28.  
  29. $recaptcha_options = [
  30. 'http' => [
  31. 'method' => 'POST',
  32. 'content' => http_build_query($recaptcha_data),
  33. ],
  34. ];
  35.  
  36. $recaptcha_context = stream_context_create($recaptcha_options);
  37. $recaptcha_result = file_get_contents($recaptcha_url, false, $recaptcha_context);
  38. $recaptcha_data = json_decode($recaptcha_result);
  39.  
  40. if (!$recaptcha_data->success) {
  41. // reCAPTCHA verification failed
  42. echo '<script>alert("reCAPTCHA verification failed. Please try again.");</script>';
  43. exit;
  44. }
  45.  
  46. if (password_verify($password, $user['password'])) {
  47. mysqli_stmt_close($stmt);
  48. header("Location: success.html");
  49. exit;
  50. }else {
  51. echo '<script>';
  52. echo '$(document).ready(()=> {';
  53. echo ' alert("Wrong password");';
  54. echo '});';
  55. echo '</script>';
  56. }
  57. } else {
  58. echo "Error: " . $conn->error;
  59. }
  60.  
  61. mysqli_close($conn);
  62. }
  63. }
  64. ?>
  65.  
  66. <!DOCTYPE html>
  67. <html lang="en">
  68. <head>
  69. <meta charset="UTF-8">
  70. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  71. <meta http-equiv="refresh" content="14">
  72. <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
  73. <link rel="stylesheet" href="form.css">
  74. <title>Login Page</title>
  75. </head>
  76. <body>
  77. <form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post">
  78. <h1>Login</h1>
  79.  
  80. <span>Username</span><input type="text" id="username" name="username" placeholder="Username">
  81. <br>
  82. <span>Password</span><input type="password" name="password" id="password" placeholder="Password">
  83. <br>
  84. <div class="g-recaptcha" data-sitekey="//site_key"></div>
  85. <br>
  86. <input type="submit" name="submit" id="submit" value="Log in">
  87. </form>
  88. <span>Dont have an account? Click to</span><button onclick="GoToPage()">register</button>
  89. <script>
  90. function GoToPage(){
  91. var a = 'index.php';
  92. window.location.href = a;
  93. }
  94. </script>
  95. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  96. </body>
  97. </html>
  98.  
Advertisement
Add Comment
Please, Sign In to add comment