<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('ketnoi.php');
header('Content-Type: application/json');

if(!isset($_SESSION['session'])) {
    echo json_encode(['total' => 0]);
    exit();
}

$session = $_SESSION['session'];
$check_user = $ketnoi->query("SELECT * FROM `users` WHERE `session` = '$session' ")->fetch_array();
if(empty($check_user['id'])) {
    echo json_encode(['total' => 0]);
    exit();
}

$username = $check_user['username'];
$result = $ketnoi->query("SELECT COUNT(*) as total FROM `history_subdomain` WHERE `username` = '$username'");

if($result) {
    $row = $result->fetch_assoc();
    echo json_encode(['total' => (int)$row['total']]);
} else {
    echo json_encode(['total' => 0]);
}
?>