<?php
  //connect to the database
  $servername = "localhost";
    $user = "rings_login";
    $password = "?Q8rriM*~[Uo";
    $database = "rings_loginsis";
    //create a connection 
    $con = mysqli_connect($servername, $user, $password, $database);

   if($con){
    ?>
    <script>
      alert("connection successful");
    </script>
    <?php
   }else{
?>
   <script>
      alert(" no connection");
    </script>
    <?php
   }
?>


<?php
// This script will handle login
session_start();

// Check if the user is already logged in
if(isset($_SESSION['username']))
{
  header("location: welcome.php");
  exit; 
}
require_once "config.php";

$username = $password = "";
$err = "";


// if request method is post 
if($_SERVER['REQUEST_METHOD'] == "POST"){
 if(empty(trim($_POST['username'])) || empty(trim($_POST['password'])))
 {
  $username_err = "Please enter username + password";
 }
 else{
  $username = trim($_POST['username']);
  $password =trim($_POST['password']);
 }

 if(empty($err))
 {
  $sql = "SELECT id, username, password FROM user WHERE username = ?";
  $stmt = mysqli_prepare($conn, $sql);
  mysqli_stmt_bind_param($stmt, "s", $param_username);
  $param_username = $username;

  // Try to excute this statement
  if(mysqli_stmt_execute($stmt)){
 mysqli_stmt_store_result($stmt);
 if(mysqli_stmt_num_rows($stmt) == 1)
 {
  mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
  if(mysqli_stmt_fetch($stmt))
  {
    if(password_verify($password, $hashed_password))
    {
      //this means the password is correct. Allow user to login
      session_start();
      $_SESSION["username"] = $username;
      $_SESSION["id"] = $id;
      $_SESSION["loggedin"] = true;

      //Redirect user to welcome page
      header("location: welcome.php");
    }
  }
 }
  }
 }
}

?>




<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <title>PHP Login System</title>
  </head>
  <body>
      <?php
  if (isset($_POST['submit'])){
    @$username = mysqli_real_escape_string($con, $_POST['username']);
   
    @$password = mysqli_real_escape_string($con, $_POST['password']);


$pass = password_hash($password, PASSWORD_BCRYPT);
    $usernamequery = "select * from user where username = '$username'";
    $query = mysqli_query($con, $usernamequery);

    $usernamecount = mysqli_num_rows($query);

    if($usernamecount > 0){
      echo "username already exists";
    }else{
      if($password === $password){

       $insertquery = "insert into  user(username, password) VALUES ('$username', '$password')";

        $iquery = mysqli_query($con, $insertquery);

        if($iquery){
           ?>
    <script>
      alert("inserted successful");
    </script>
    <?php
   }else{
   ?>
   <script>
      alert(" no connection");
    </script>
    <?php
   }
      }else{
        echo "password are not matching";
      }
    }
  }
  ?>
   <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Php Login System</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/register.php">Register</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/login.php">Login</a>
        </li>
       <li class="nav-item">
          <a class="nav-link" href="/logout.php">Logout</a>
        </li>
      </ul>
    </div>
    </div>

</nav>

<div class="container mt-4">
  <h3>Please Login Here</h3>
  <hr>
  <form action="" method="post">
    <div class="form-group">
     <label for="exampleInputEmail1">Username</label>
     <input type="text" name="username" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Username">
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Password</label>
      <input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="Enter Password">
    </div>
    <div class="form-group form-check">
      <input type="checkbox" class="form-check-input" id="exampleCheck1">
      <label class="form-check-label" for="exampleCheck1">Check me out</label>
       </div>
       <button type="submit" class="btn btn-primary">Submit</button>
  </form>
</div>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
    -->
  </body>
</html>