skip to Main Content
PHP Set Password Protection File Like Htaccess

PHP set password protection file like htaccess

Many times we need to set a password on single file like Adminer, PHPINFO etc… We don’t want to share a access to those files with any normal user. So, Basically, we set Password protection using htaccess or web config on those files. PHP provides the easiest way to set Password on a single file. let’s see using PHP set password protection file like htaccess.

Let’s open the PHP file then copy past below PHP code.

PHP set password protection file

$LoginSuccessful = false;

if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){

    $Username = $_SERVER['PHP_AUTH_USER'];
    $Password = $_SERVER['PHP_AUTH_PW'];

    if ($Username == 'setusername' && $Password == 'setpa55word'){
        $LoginSuccessful = true;
    }
}

if (!$LoginSuccessful){ 

    header('WWW-Authenticate: Basic realm="Secret page"');
    header('HTTP/1.0 401 Unauthorized');

    print "Login failed!\n";
    die();

}

After, Copy that code just change the username and password then check you can’t able to access those file without username and password.

If you have any question write the comment below.

Saroj

I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top