Bladephp: Now with 7+ codeigniter, php, and laravel project 7+ php project From $18/Year From Rs. 1000/Year

How to Simple HTTP Authentication using PHP

by
Category:php
php html
How to Simple HTTP Authentication using PHP

That’s all it takes to have the little box show up and demand a username and password. Now all you have to do is check the submitted username and password against the correct ones. Simply check the $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] global variables. You may also like How to create login authentication in codeigniter.

HTML and PHP Code

<?php
$authenticate = false;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
    $name = $_SERVER['PHP_AUTH_USER'];
    $pass = $_SERVER['PHP_AUTH_PW'];
    if ($name == 'user' && $pass == 'pass') {
        $authenticate = true;
    }
}

if ($authenticate == false) {
    header('WWW-Authenticate: Basic realm="Restricted Page Enter Details To Continue"');
    header('HTTP/1.0 401 Unauthorized');
    echo "Authentication Failed Refresh To Do It Again";
} else {
    ?>
    <html>
        <body>
            <h1>How to Simple HTTP Authentication using PHP</h1>
            <p>All Your Content Comes Here</p>
        </body>
    </html>
    <?php
}
?>

0 Comments


Html code work in comment box


Related Post