PHP file based authentication

In this post, you will learn how to implement file-based authentication using the PHP programming language.

Some small sites do not have a need for a database back-end to store data. But security is still important, whether the site is big or small. They may have a requirement to authenticate some folder or file and want to set access credentials for that. We can handle such a case by using file based authentication using PHP.

To create the file-based authentication, first generate a username and password set by using the PHP password encryption technique md5(). The md5() function is commonly used to encrypt a string.
You can generate the username and password on click here.

Create a text file "auth.txt" and paste the generated username and password set as it is in the text file. Now, create a login form and copy and paste these code to authenticate the username and password from a text file.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<div class="wrapper" style="width: 30%; margin: 0 auto;">
    <form class="form-signin" action='#' method="post">       
      <h2 class="form-signin-heading">User Login</h2><br/>
      <input type="text" class="form-control" name="username" placeholder="username" required="" autofocus="" /><br/>
      <input type="password" class="form-control" name="password" placeholder="Password" required=""/><br/>
      <button class="btn btn-small btn-primary btn-block" type="submit">Login</button>  
    function check_password($username, $password){
        $pwd_file = 'auth.txt';
        if(!$fh = fopen($pwd_file, "r")) {die("<p>Could not open password file");}
        $match = 0;
        $pwd = md5($password);
        while(!feof($fh)) {
          $line = fgets($fh, 4096);
          $user_pass = explode(":", $line);
          if($user_pass[0] == $username) {
            if(rtrim($user_pass[1]) == $pwd) {
              $match = 1;
          $match = 2; 
        if($match == '1') {
           echo "<b>Login Success!</b>";
        if($match == '2') {
           echo "<b>Login Failed!</b>";
    if($_POST['username']) {
        check_password($_POST['username'], $_POST['password']);

In the above code, we have used the fopen() function to open the text file and encrypt the entered password using the md5() function. We have matched the encrypted password with the stored password. If both passwords are the same, it prints a success message.

Related Articles

PHP Server Side Form Validation
File Upload Validation in PHP
PHP File Upload MIME Type Validation with Error Handling
Complete HTML Form Validation in PHP
PHP sanitize input for MySQL
How to encrypt password in PHP
PHP7 Password Hashing
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
Simple pagination in PHP
Simple PHP File Cache
PHP Connection and File Handling on FTP Server
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP

Read more articles

General Knowledge

Learn Popular Language