I guess you know the song. I was looking today for an alternative method to build an universal admin login method and plug it in my site framework. The ideal method would be the same for any site, user friendly and easy for me to handle in the background. It should remeber the password and fit into any control panel I decide to build. Not to mention cross-browser compatibility!
After thinking and thinking… what is better than the default authentication method hardcoded in the HTTP Protocol?
It's simple, it can contain a message where I marked it, it can remeber passwords, can take username and password and everybody knows how to use it. It rules my lazy world! Well, you might think it's a breeze getting this to work and it should be as stated here. Yeah! Sure!
Last night I spent 1 whole freaking hour trying to figure out what was wrong with my flawless piece of code (that compacted PHP code = multiple files into one, comments stripped and spaces removed.) just to learn that PHP takes # along // as single line comments. I've been coding in PHP for about 5 years and never have I noticed that # is also a comment.
Today I got another treat. HTTP 401 Auth does not work in PHP CGI Mode and that's how I use it. The expected fields: PHP_AUTH_USER, PHP_AUTH_PW are not filled in and they don't get filled in as supposed to. So, guess what…
Everybody is bitching (with every right to do so) about this problem in the PHP dev community. But the fix is rather simple. It involves some .htaccess handling and tuning to pass the Authentication http header to the PHP script. It all looked simple now but there's another problem I faced.
Instead of getting REDIRECT_HTTP_AUTHORIZATION as a $_REQUEST['REDIRECT_HTTP_AUTHORIZATION'] variable I was getting REDIRECT_REDIRECT_HTTP_AUTHORIZATION. Wtf? I researched and others saw enev more then two REDIRECT_ in the variable.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^admin\. #[optional host restriction]
RewriteCond %{HTTP:Authorization} !^$ #must exist
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
The optional host restriction allows you to assign the Auth info only for a subdomain starting with e.g.: admin. It's optional and removable. This code pushes in an extra header: HTTP_AUTHORIZATION. Which get REDIRECT_ appended to it for whatever reason.
You can now handle the login process and act as you wish. Combine with mySql for authentication. Good luck! I'm sure from here on your admin login screen will be the easiest task of any control panel.
PS: There's a small problem with this method. You can't logout! So what? I don't use cPanels in public spaces and neither should you:)