Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Created December 6, 2021 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BhargavBhandari90/3b0c98760fd0c442d62a5423de966f13 to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/3b0c98760fd0c442d62a5423de966f13 to your computer and use it in GitHub Desktop.
Add HTTP auth to perticular file PHP.
<?php
// Http auth start.
$valid_passwords = array( 'bunty' => 'Bunty@123' ); // Key is username and value is password.
$valid_users = array_keys( $valid_passwords ); // Get all usernames from $valid_passwords array.
$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
// Check if user is valid.
$validated = ( in_array( $user, $valid_users ) ) && ( $pass == $valid_passwords[ $user ] );
if ( ! $validated ) {
header( 'WWW-Authenticate: Basic realm="Restricted File"' );
header( 'HTTP/1.0 401 Unauthorized' );
die( 'Not authorized' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment