Basic Authentication CGI

We are running our site on SiteGround. By default SiteGround uses CGI/FastCGI as the PHP Server API. The only problem with that is that Basic Authentication doesn’t work, or doesn’t work as it should with CGI. The PHP_AUTH_USER and PHP_AUTH_PW variables used to contain the authentication is just not present, and the HTTP_AUTHORIZATION server variable is not translated.


These were the steps we had to take to get around it:

Add an entry into the htaccess file (the RewriteEngine line may not be needed):

<IfModule mod_rewrite.c>

RewriteEngine on

SetEnvIf Authorization “(.*)” HTTP_AUTHORIZATION=$1

</IfModule>

This populates the HTTP_AUTHORIZATION server variable.

then – inside the code in the file at /wp-content/plugins/affiliate-wp/includes/REST/class-rest-authentication.php – add a line which translates the decoded HTTP_AUTHORIZATION into server variables.

public function authenticate( $user_id ) {

list($_SERVER[‘PHP_AUTH_USER’], $_SERVER[‘PHP_AUTH_PW’]) = explode(‘:’ , base64_decode(substr($_SERVER[‘HTTP_AUTHORIZATION’], 6)));

if ( ! empty( $user_id ) ) { //|| empty( $_SERVER[‘PHP_AUTH_USER’] ) ) {