HTTP Authentication When PHP Runs As A CGI/PHPSuExec

PHP is a feature-rich programming language that includes simple HTTP authentication. Unfortunately, if PHP is running under CGI then PHP scripts cannot use HTTP authentication. The workaround for this problem is to use mod_rewrite to pass HTTP authentication info to scripts as a GET parameter.

The following are step-by-step instructions on how to get HTTP authentication working when PHP is running as a CGI/PHPSuExec.

Step 1:
Create a plain text file using a text editor such as Windows Notepad.

Step 2:
Add the following text...


<IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


Step 3:
Save the file as a plain text file. Name the file ".htaccess"

Step 4:
Upload the .htaccess file in ASCII mode to your web space using an FTP application.

Step 5:
Add the following in your PHP script right before your user/pass check routine.


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




// split user/pass parts
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));

// open user/pass prompt
if (!isset($_SERVER['PHP_AUTH_USER'])) {
   header('WWW-Authenticate: Basic realm="Your Realm"');
   header('HTTP/1.0 401 Unauthorized');
   echo 'Text to send if Cancel button is used';
 } else {
   echo "<p>Greetings: </p>".$_SERVER['PHP_AUTH_USER'];
   echo "<p>Password you entered: </p>".$_SERVER['PHP_AUTH_PW'];


Check out our webspace hosting packages today.


Official PHP Website
PHP Scripts
PHP Webspace Hosting

PHP Webspace Hosting

PHP Webspace Hosting


Crontab Cron Jobs support is free and is included in all webspace hosting packages. You can easily setup custom Crontab Cron Jobs using the control panel.



Webspace Hosting Best Value
Webspace Customers provides quality webspace hosting at one of the best prices in the webspace hosting industry. By utilizing the latest open source software and by eliminating all unnecessary costs, we can provide our clients with cheap highly reliable webspace.

Webspace Hosting Quality Features
By choosing one of our webspace hosting packages, you will receive countless quality features providing everything your website needs to flourish and grow. You are not limited to only using your account for personal webspace with our service. With our webspace hosting you can run a business website, install shopping carts, customer service and technical support scripts, forums, image galleries, chat rooms, and more all for free at the click of the mouse.
Reliable Webspace Hosting
You deserve webspace with unwavering reliability. Our robust network infrastructure ensures superior performance and maximum uptime for our webspace hosting packages. Our network architecture uses Enterprise routing and switching engines from Cisco and Juniper. We use Juniper M20 routers as border routers, Cisco 6500 series switches in our distribution layer and Cisco 6500 switches in our aggregation layers, and Cisco 3500 and 2900 series switches at the client layer. Our network is fully meshed with redundant backbone providers for a total of 100 gigabits /second of bandwidth.
Webspace Hosting Technical Support
Being the leading webspace hosting provider in the industry, our goal is to provide our customers fasst comprehensive resolutions to problems. Our technical support help desk system routes technical support inquires directly to support team members that are best able to resolve your issue. Our current average help desk response time is 12 minutes 59 seconds.