<?php

$servername = "localhost";

$database = "hataccounts_sampath";

$username = "hataccounts_sampath";

$password = "Sam@77557";





// Create connection

$conn = mysqli_connect($servername, $username, $password, $database);



 if (!$conn) {

      die("Connection failed: " . mysqli_connect_error());

 }

if (isset($_COOKIE['user']) && !isset($_SESSION["normal_user"])) {
     $user = json_decode($_COOKIE['user'], true);

     $uname = htmlspecialchars(trim($user['normal_user']), ENT_QUOTES, 'UTF-8');
     $sql = mysqli_query($conn, "SELECT * FROM details_of_users WHERE user='$uname'");

     $row = mysqli_fetch_assoc($sql);

     if (mysqli_num_rows($sql) == 1) {

          $_SESSION['normal_user'] = $user['normal_user'];
          $_SESSION['normal_user_id'] = $user['normal_user_id'];
          $_SESSION['val'] = $user['val'];
     }
}

if (!isset($_SESSION['temp_user'])) {

     $sql3 = "SELECT * FROM invoice_no";

     $result3 = mysqli_query($conn, $sql3);

     $row = mysqli_fetch_assoc($result3);

     $_SESSION['temp_user'] = $row['token'];
     $x = $row['token'];
     $sql4 = "UPDATE invoice_no SET token='" . ++$x . "'";

     $result4 = mysqli_query($conn, $sql4);
}


if (isset($_COOKIE['user_admin']) && !isset($_SESSION["user"])) {
     $user_x = json_decode($_COOKIE['user_admin'], true);

     $uname = htmlspecialchars(trim($user_x['user']), ENT_QUOTES, 'UTF-8');

     $sql = mysqli_query($conn, "SELECT * FROM details_of_users WHERE user='$uname'");

     $row = mysqli_fetch_assoc($sql);

     if (mysqli_num_rows($sql) == 1) {

          $_SESSION['user'] = $user_x['user'];
     }
}

?>