Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $serverName = "localhost"; //Variables to access the user database
- $username = "root";
- $password = "";
- $database = "snake_database";
- $errors = []; //Array of all the errors to display to the user
- $conn = mysqli_connect($serverName, $username, $password, $database); //Connect to the database
- if(!$conn){ //If the database failed to connect
- die("Database failed to connect: " .mysqli_connect_error()); //Display an error message
- }
- $username = $_POST['username']; //set the username/ password varaibles
- $password = $_POST['password'];
- $hashPass = password_hash($password, PASSWORD_DEFAULT); //Encrypt the password
- $sql = "SELECT * FROM users WHERE username = ?"; //Select all usernames and passwords
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("s", $username);
- $stmt->execute();
- $result = $stmt->get_result();
- if (password_verify($password, $userRow['password']))
- {
- $count = 1;
- }
- else
- {
- $count = 0;
- }
- if($count == 1) //If there is 1 account that matches
- {
- $stmt->close(); //Close the statment and connection
- $conn->close();
- session_start();
- $_SESSION["LoggedUser"] = $username; //Log the user in
- $_SESSION["lastPage"] = "login.php";
- header("location: profile.php"); //Direct the user to their profile
- }else //if there is no accounts that match
- {
- array_push($errors, "Username or password is incorrect");
- session_start();
- $_SESSION["loginErrors"] = $errors;
- $_SESSION["lastPage"] = "login.php"; //Make this page the last page
- header("location: index.php"); //Go to the homepage
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement