Website Lock / Password Protected Website PHP & CSS Code

PHP Code – Website Lock System

WordPress Dashboard → Appearance → Theme File Editor → Child Theme → functions.php open karein.

Ye code add karein:


/* ====================================
   WORDPRESS FULL WEBSITE PASSWORD LOCK
==================================== */

// Change your website password here
define('MY_WEBSITE_PASSWORD', 'Pramod@12345');

// Check if visitor has unlocked website
function my_website_password_unlocked() {

    // Admin can access without password
    if (
        is_user_logged_in() &&
        current_user_can('manage_options')
    ) {
        return true;
    }

    // Check secure access cookie
    if (empty($_COOKIE['my_website_access'])) {
        return false;
    }

    $token = sanitize_text_field(
        wp_unslash($_COOKIE['my_website_access'])
    );

    if (!preg_match('/^[a-f0-9]{64}$/', $token)) {
        return false;
    }

    return (bool) get_transient(
        'my_website_token_' . hash('sha256', $token)
    );
}


// Protect entire public website
add_action('template_redirect', function () {

    // Allow WordPress admin dashboard
    if (is_admin() || wp_doing_ajax()) {
        return;
    }

    // Allow administrator
    if (my_website_password_unlocked()) {
        return;
    }

    // Password form submission
    if (
        $_SERVER['REQUEST_METHOD'] === 'POST' &&
        isset($_POST['my_website_password'])
    ) {

        // Security verification
        if (
            !isset($_POST['my_website_nonce']) ||
            !wp_verify_nonce(
                sanitize_text_field(
                    wp_unslash($_POST['my_website_nonce'])
                ),
                'my_website_unlock'
            )
        ) {
            wp_die('Security verification failed. Refresh and try again.');
        }

        $entered_password = (string) wp_unslash(
            $_POST['my_website_password']
        );

        if (hash_equals(MY_WEBSITE_PASSWORD, $entered_password)) {

            // Create secure random access token
            $token = bin2hex(random_bytes(32));

            // Save token on server for 12 hours
            set_transient(
                'my_website_token_' . hash('sha256', $token),
                1,
                12 * HOUR_IN_SECONDS
            );

            // Set secure browser cookie
            setcookie(
                'my_website_access',
                $token,
                array(
                    'expires' => time() + (12 * HOUR_IN_SECONDS),
                    'path' => '/',
                    'secure' => is_ssl(),
                    'httponly' => true,
                    'samesite' => 'Lax'
                )
            );

            // Reload website after successful login
            wp_safe_redirect(home_url('/'));
            exit;

        } else {

            $GLOBALS['my_website_wrong_password'] = true;

        }
    }

    // Display lock screen
    status_header(200);
    nocache_headers();

    ?>
    <!DOCTYPE html>
    <html>
    <head>

        <meta charset="UTF-8">

        <meta name="viewport"
              content="width=device-width, initial-scale=1">

        <title>Website Password Protected</title>

        <?php wp_head(); ?>

    </head>

    <body>

        <div class="website-password-lock">

            <div class="password-lock-box">

                <div class="password-lock-icon">🔒</div>

                <h1>Website Locked</h1>

                <p>
                    This website is password protected.
                    Please enter your password to continue.
                </p>

                <?php
                if (!empty($GLOBALS['my_website_wrong_password'])):
                ?>

                    <div class="password-error">
                        Incorrect Password! Try Again.
                    </div>

                <?php endif; ?>

                <form method="POST">

                    <?php
                    wp_nonce_field(
                        'my_website_unlock',
                        'my_website_nonce'
                    );
                    ?>

                    <input
                        type="password"
                        name="my_website_password"
                        placeholder="Enter Website Password"
                        required
                        autocomplete="current-password"
                    >

                    <button type="submit">
                        Unlock Website
                    </button>

                </form>

                <div class="password-lock-footer">
                    Secure Website Access
                </div>

            </div>

        </div>

        <?php wp_footer(); ?>

    </body>
    </html>
    <?php

    exit;

}, 1);

CSS Code – Premium Lock Screen Design

Ab Appearance → Customize → Additional CSS mein ye code paste karein.


/* FULL WEBSITE PASSWORD LOCK DESIGN */

.website-password-lock {
    min-height: 100vh;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    box-sizing: border-box;

    background:
    radial-gradient(
        circle at top,
        #263d60,
        #101b30 65%,
        #080e1a
    );
}

.password-lock-box {
    width: 100%;
    max-width: 440px;
    padding: 45px 35px;
    box-sizing: border-box;
    text-align: center;

    background: linear-gradient(
        145deg,
        #1c2d47,
        #101b2d
    );

    border: 1px solid rgba(220,190,130,.4);
    border-radius: 22px;

    box-shadow: 0 25px 70px rgba(0,0,0,.4);

    color: #fff;
}

.password-lock-icon {
    font-size: 65px;
    margin-bottom: 20px;
}

.password-lock-box h1 {
    color: #fff;
    font-size: 30px;
    margin: 0 0 15px;
}

.password-lock-box p {
    color: #b6c4d7;
    font-size: 14px;
    line-height: 1.7;
    margin-bottom: 25px;
}

.password-lock-box input {
    width: 100%;
    height: 55px;
    padding: 0 15px;
    box-sizing: border-box;

    background: #0b1525;
    border: 1px solid #42516a;
    border-radius: 10px;

    color: #fff;
    font-size: 15px;
    margin-bottom: 15px;
}

.password-lock-box input:focus {
    outline: none;
    border-color: #dfbd7e;
}

.password-lock-box button {
    width: 100%;
    height: 55px;
    border: none;
    border-radius: 10px;

    background: linear-gradient(
        90deg,
        #c69c5d,
        #efd39c
    );

    color: #142034;
    font-size: 15px;
    font-weight: bold;
    cursor: pointer;
}

.password-lock-box button:hover {
    filter: brightness(1.1);
}

.password-error {
    background: #572c35;
    color: #ffd2d2;
    padding: 12px;
    margin-bottom: 15px;
    border-radius: 8px;
}

.password-lock-footer {
    margin-top: 25px;
    color: #8fa0b7;
    font-size: 12px;
}

@media(max-width:480px) {

    .password-lock-box {
        padding: 35px 22px;
    }

    .password-lock-box h1 {
        font-size: 25px;
    }

}

Add Image Code :


background-image: url('https://example.com/your-background.jpg');
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;

Add Video Code:

Apne functions.php mein lock screen ke existing HTML mein <body> ke turant baad ye code add karein:


<video class="lock-video-bg" autoplay muted loop playsinline>
    <source src="YOUR-VIDEO-URL.mp4" type="video/mp4">
</video>

<div class="lock-video-overlay"></div>

Appearance → Customize → Additional CSS mein ye code paste karein:


/* VIDEO BACKGROUND */

.lock-video-bg {
    position: fixed;
    top: 0;
    left: 0;

    width: 100%;
    height: 100%;

    object-fit: cover;
    z-index: -2;
}

/* DARK OVERLAY */

.lock-video-overlay {
    position: fixed;
    inset: 0;

    background: rgba(0, 0, 0, 0.50);

    z-index: -1;
}

/* LOCK SCREEN */

.website-password-lock {
    position: relative;
    z-index: 1;

    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;

    background: transparent;
}

Leave a Comment

Prove your humanity: 8   +   3   =