<?php
session_start();
$conn = new mysqli("localhost","root","","breakout_db");
if($conn->connect_error){ die("DB Error"); }

// Register
if(isset($_POST['register'])){
    $u = $_POST['user'];
    $p = password_hash($_POST['pass'], PASSWORD_DEFAULT);
    $conn->query("INSERT INTO users(username,password,highscore) VALUES('$u','$p',0)");
}

// Login
if(isset($_POST['login'])){
    $u = $_POST['user'];
    $p = $_POST['pass'];
    $res = $conn->query("SELECT * FROM users WHERE username='$u'");
    if($row = $res->fetch_assoc()){
        if(password_verify($p, $row['password'])){
            $_SESSION['user'] = $u;
            header("Location: index.php");
            exit;
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="login-box">
<h2>Login</h2>
<form method="POST">
<input name="user" placeholder="Username" required>
<input name="pass" type="password" placeholder="Password" required>
<button name="login">Login</button>
</form>

<h2>Register</h2>
<form method="POST">
<input name="user" placeholder="Username" required>
<input name="pass" type="password" placeholder="Password" required>
<button name="register">Register</button>
</form>
</div>
</body>
</html>