HTTP Authentication with PHP running as CGI

When I used HTTP Basic Authentication for my API, I just found it is not working on the server, but it is working on my localhost.

The reason is HTTP Authorization does not work when your PHP is installed and working as CGI. It works perfectly when PHP is installed as a module though. You can check PHP info to find which way is you php installed.

But we always can have a solution.

reference article:

My code:

.htaccess

PHP

Need notice:

Depending on your apache version the variable you rewrite can be prepended with ‘REDIRECT_’.

That is if you’re using HTTP_AUTHORIZATION the real variable in _SERVER becomes REDIRECT_HTTP_AUTHORIZATION.

Final in PHP