Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. function login(){
  2. var username = document.getElementById("username").value;
  3. var password = document.getElementById("password").value;
  4. var dataString = 'username1=' + username + '&password1=' + password;
  5. if (username == '' || login == ''){
  6. window.alert("Please fill in username or password.");
  7. }
  8. else{
  9. $.ajax({
  10. type: "POST",
  11. url: "login.php",
  12. data: dataString,
  13. cache: false,
  14. crossDomain : true,
  15. success: function(response) {
  16. if (response.status == 200) {
  17. window.location = 'http://localhost/site.html';
  18. }
  19. else {
  20. window.alert("The password or username you have entered is not valid");
  21. }
  22. }
  23. });
  24. }
  25. return false;
  26.  
  27. <?php
  28. header('Access-Control-Allow-Origin: *');
  29. header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
  30. header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
  31. $password2 = $_POST['password1'];
  32. $username2 = $_POST['username1'];
  33. $connection = mysqli_connect("localhost", "root", "password", "database") or die("Unable to connect to MySQL");
  34. $query = mysqli_query($connection, "SELECT * FROM users where username = '$username2' AND password = '$password2'") or die(mysqli_error($connection));
  35. $row = mysqli_fetch_array($query, MYSQLI_BOTH) or die(mysqli_error($connection));
  36. if(!empty($row['username']) AND !empty($row['password'])) {
  37. session_start();
  38. $_SESSION['username'] = $username2;
  39. http_response_code(200);
  40. echo "Successful Login";
  41. exit;
  42. }
  43. else{
  44. http_response_code(403);
  45. echo "The password or username you have entered is not valid";
  46. }
  47. mysqli_close($connection);
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement