<?php
if(!isset($_COOKIE['id']))
{
    echo "<script>window.location.href = 'index.php';</script>";
}
else
{


session_start();
$host = "blueris.beget.tech";
$username = "blueris_base";
$password = "Qwer123.";
$database = "blueris_base";
$mysql = new mysqli($host, $username, $password, $database);

$login = $_POST["login"];
if($mysql->connect_error){
    die("Ошибка подключения". $mysql->connect_error);
}


// Получение данных пользователя из базы данных
session_start();
if (isset ($login))
if (isset ($_COOKIE['id']))
$user_id = $_COOKIE['id'];
$userlogin = $_SESSION['login'];
$quary = "SELECT * FROM login WHERE login like'$login'";
$result = mysqli_query($mysql, $quary);
if(mysqli_num_rows($result) > 0 && $userlogin == $login)
{
    header('Location: changelogin.php?message=Логины+совпадают');
    echo "<script>window.location.href = 'changelogin.php';</script>";     
    mysqli_close();
    session_abort();
}
if(mysqli_num_rows($result) > 0 && $userlogin != $login)
{
    header('Location: changelogin.php?message=Логин+существует');
    echo "<script>window.location.href = 'changelogin.php';</script>";  
    mysqli_close();
    session_abort();
}
session_abort();
if($userlogin != $login && mysqli_num_rows($result) == 0)
{
$query = "UPDATE login SET login = '$login' WHERE id_user = $user_id";
if($res = mysqli_query($mysql, $query))
{
    session_start();
    $_SESSION['login'] = $login;
    echo "<script>window.location.href = 'Account.php';</script>";     
}
}
}
?>