Как закрыть паролем просмотр обычного HTML сайта?

Как закрыть паролем просмотр обычного HTML сайта
Как закрыть паролем просмотр обычного HTML сайта

Для того, чтобы просто и без заморочек закрыть обычный сайт состоящий из простых html нужно сделать несколько правок:

Создаем или редактируем index.html
добавляем скрипт в body или целиком файл:

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Вход</title>
    <style>
        body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #f4f4f4; }
        .box { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); text-align: center; }
        input { padding: 10px; width: 200px; margin-bottom: 10px; display: block; margin: 10px auto; border: 1px solid #ccc; }
        button { padding: 10px 20px; background: #28a745; color: white; border: none; cursor: pointer; border-radius: 4px; font-size: 16px; }
        .err { color: red; margin-top: 10px; }
    </style>
</head>
<body>
    <div class="box">
        <h3>Доступ закрыт</h3>
        <input type="password" id="p" placeholder="Пароль" autofocus>
        <button onclick="check()">Войти</button>
        <p class="err" id="e" style="display:none;">Неверный пароль!</p>
    </div>

    <script>
        const PASS = "558800";

        function check() {
            if (document.getElementById('p').value === PASS) {
                // Ставим cookie на 7 дней
                document.cookie = "access=granted; path=/; max-age=604800";
                window.location.href = 'stranica-pocle-vhoda.html';
            } else {
                document.getElementById('e').style.display = 'block';
            }
        }

        document.getElementById('p').addEventListener('keyup', e => {
            if (e.key === 'Enter') check();
        });

        // Если уже есть cookie, сразу кидаем на сайт
        if (document.cookie.includes('access=granted')) {
            window.location.href = 'stranica-pocle-vhoda.html';
        }
    </script>
</body>
</html>

Следующий шаг:

на всех внутренних страницах, которые нужно закрыть

в head добавляем

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

<script>
    // Проверяем куки ПРЯМО СЕЙЧАС
    if (!document.cookie.includes('access=granted')) {
        // Если куки нет — жестко перезагружаем страницу на вход
        window.location.replace('/index.html');
    }
</script>

и можно добавить временно кнопку выхода

перед закрывающим Body



<button onclick="logout()" style="    padding: 8px 16px;
    background: #dc3545;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    position: fixed;
    bottom: 0;
    right: 0;">Выйти</button>

<script>
function logout() {
    document.cookie = "access=; path=/; max-age=0";
    window.location.href = '/index.html';
}
</script>

guest
Другие сообщения форума
Закрыть меню
ZAPLATA.RU | Магазин готовых блогов Wordpress с автонаполнением
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.