Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function login(){
- var username = document.getElementById("username").value;
- var password = document.getElementById("password").value;
- var dataString = 'username1=' + username + '&password1=' + password;
- if (username == '' || login == ''){
- window.alert("Please fill in username or password.");
- }
- else{
- $.ajax({
- type: "POST",
- url: "login.php",
- data: dataString,
- cache: false,
- crossDomain : true,
- success: function(response) {
- if (response.status == 200) {
- window.location = 'http://localhost/site.html';
- }
- else {
- window.alert("The password or username you have entered is not valid");
- }
- }
- });
- }
- return false;
- <?php
- header('Access-Control-Allow-Origin: *');
- header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
- header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
- $password2 = $_POST['password1'];
- $username2 = $_POST['username1'];
- $connection = mysqli_connect("localhost", "root", "password", "database") or die("Unable to connect to MySQL");
- $query = mysqli_query($connection, "SELECT * FROM users where username = '$username2' AND password = '$password2'") or die(mysqli_error($connection));
- $row = mysqli_fetch_array($query, MYSQLI_BOTH) or die(mysqli_error($connection));
- if(!empty($row['username']) AND !empty($row['password'])) {
- session_start();
- $_SESSION['username'] = $username2;
- http_response_code(200);
- echo "Successful Login";
- exit;
- }
- else{
- http_response_code(403);
- echo "The password or username you have entered is not valid";
- }
- mysqli_close($connection);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement