Guest User

Untitled

a guest
Jan 18th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. Notice: Undefined variable: db in C:xampphtdocslogin.php on line 34
  2.  
  3. Fatal error: Call to a member function prepare() on null in C:xampphtdocslogin.php on line 34
  4.  
  5. <?php
  6. include 'inc/base.php';
  7.  
  8. function captcha() {
  9. $url = 'https://www.google.com/recaptcha/api/siteverify';
  10. $privatekey = '6LdXGA8TAAAAAEItg6VkhIwYXswy3zoJYkSsa2bT';
  11. $response = file_get_contents($url."?secret=".$privatekey."&response=".strip_tags($_POST['g-recaptcha-response'])."&remoteip=".$_SERVER['REMOTE_ADDR']);
  12. $data = json_decode($response);
  13. return isset($data->success) AND $data->success == true;
  14. }
  15.  
  16. // LOGIN
  17. if (isset($_POST['login'], $_POST['username'], $_POST['password'])) {
  18. $query = $db->prepare('SELECT COUNT(*) AS nb, id, username FROM users WHERE username=? AND password=?');
  19. $query->execute([
  20. $_POST['username'],
  21. md5($_POST['password'])
  22. ]);
  23. $data = $query->fetch();
  24. if ($data->nb) {
  25. $_SESSION['user'] = $data->username;
  26. $_SESSION['user_id'] = $data->id;
  27. header("location: index.php");
  28. exit;
  29. } else {
  30. $error = "Password or username incorrect";
  31. }
  32. }
  33.  
  34. // SIGNUp
  35. if (isset($_POST['signup'], $_POST['username'], $_POST['email'], $_POST['password'], $_POST['confirm'])) {
  36. if (preg_match("#^[a-z0-9_-]{3,30}$#i", $_POST['username'])) {
  37. if (captcha()) {
  38. $query = $db->prepare("SELECT COUNT(*) AS nb FROM users WHERE username=?");
  39. $query->execute([
  40. $_POST['username']
  41. ]);
  42. $data = $query->fetch();
  43.  
  44. if (!$data->nb) {
  45. if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  46. $query = $db->prepare("SELECT COUNT(*) AS nb FROM users WHERE email=?");
  47. $query->execute([
  48. $_POST['email']
  49. ]);
  50. $data = $query->fetch();
  51.  
  52. if (!$data->nb) {
  53. if ($_POST['password'] === $_POST['confirm']) {
  54. if (strlen($_POST['password']) >= 4) {
  55.  
  56. $query = $db->prepare("INSERT INTO users(username, email, password) VALUES(:username, :email, :password)");
  57. $query->execute([
  58. "username" => $_POST['username'],
  59. "email" => $_POST['email'],
  60. "password" => md5($_POST['password'])
  61. ]);
  62.  
  63. $_SESSION['user'] = $_POST['username'];
  64. $_SESSION['user_id'] = $db->lastInsertId();
  65.  
  66. header("Location: index.php");
  67. exit;
  68.  
  69. } else {
  70. $error = "The password must be at least 4 characters";
  71. }
  72. } else {
  73. $error = "The two passwords must match";
  74. }
  75. } else {
  76. $error = "This email is already taken";
  77. }
  78. } else {
  79. $error = "Incorrect email format";
  80. }
  81. } else {
  82. $error = "This username is already taken";
  83. }
  84. }
  85. else
  86. {
  87. $error = "Invalid captcha";
  88. }
  89. }
  90. else
  91. {
  92. $error = "The username must be within 3 and 30 characters";
  93. }
  94. }
  95.  
  96. include 'inc/header.php';
  97. ?>
  98. <?php
  99. if (isset($error)) echo "<div class="message_erreur">$error</div>";
  100. ?>
  101. <section>
  102. <h1 class="title">Log in</h1>
  103. <form method="POST">
  104. Username : <input type="text" name="username" required />
  105. Password : <input type="password" name="password" required />
  106. <input type="submit" name="login" value="Se connecter" required />
  107. </form>
  108. </section>
  109.  
  110. <section>
  111. <h2 class="title">Sign Up</h2>
  112. <form method="POST">
  113. <label>Username :<br />
  114. <input type="text" name="username" <?php if(isset($_POST['username'])) echo 'value="'.htmlspecialchars($_POST['username']).'"'; ?> required />
  115. </label>
  116. <label>Email :<br />
  117. <input type="text" name="email" <?php if(isset($_POST['email'])) echo 'value="'.htmlspecialchars($_POST['email']).'"'; ?> required />
  118. </label>
  119. <label>Password :<br />
  120. <input type="password" name="password" required />
  121. </label>
  122. <label>Repeat password :<br />
  123. <input type="password" name="confirm" required />
  124. </label>
  125. <center><div class="g-recaptcha" data-sitekey="6LdXGA8TAAAAAKV0MvCvn5eT3--7v4eipyvKPD0s"></div></center><br/>
  126. <input type="submit" name="signup" value="Créer un compte" />
  127. </form>
  128. </section>
  129. <?php
  130. include 'inc/footer.php';
Add Comment
Please, Sign In to add comment