Deprecated: Non-static method DB::connect() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/SNF/users.inc on line 1120

Deprecated: Non-static method DB::parseDSN() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 520

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 551

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 557

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/SNF/users.inc on line 1122

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1347

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1009

Deprecated: Non-static method DB::isManip() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 2195

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1014

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1356

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 1387

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/SNF/users.inc on line 1130

Deprecated: Non-static method DB::connect() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/wiki.inc on line 38

Deprecated: Non-static method DB::parseDSN() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 520

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 551

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 557

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/wiki.inc on line 40

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1347

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1009

Deprecated: Non-static method DB::isManip() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 2195

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1014

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1356

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 1387

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/wiki.inc on line 48

Deprecated: Non-static method DB::connect() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 227

Deprecated: Non-static method DB::parseDSN() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 520

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 551

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 557

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 229

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1347

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1009

Deprecated: Non-static method DB::isManip() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 2195

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1014

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1356

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 1387

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 238

Deprecated: Non-static method DB::connect() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 265

Deprecated: Non-static method DB::parseDSN() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 520

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 551

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 557

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 267

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1347

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1009

Deprecated: Non-static method DB::isManip() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 2195

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1014

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1356

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 1387

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 276

Deprecated: Non-static method DB::connect() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 451

Deprecated: Non-static method DB::parseDSN() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 520

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 551

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 557

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 453

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1283

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1009

Deprecated: Non-static method DB::isManip() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 2195

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1014

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB/common.php on line 1292

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/DB.php on line 1387

Deprecated: Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /home/fhazel/include/icky/article.inc on line 461
SNF wiki: HTTP authentication with PHP-CGI

HTTP authentication with PHP-CGI


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 702

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 702

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 720

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 1143

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 1143

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 1143

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 1143

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 1143

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/fhazel/lib/php/markdown.101c.php on line 1143

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser.php on line 180

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser.php on line 180

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser.php on line 180

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser.php on line 180

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser.php on line 180

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser.php on line 180

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser.php on line 180

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser/Filter/Links.php on line 69

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser/Filter/Lists.php on line 80

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser/Filter/Images.php on line 72

Deprecated: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in /home/fhazel/pear/php/HTML/BBCodeParser/Filter/Email.php on line 69

HTTP authentication is ugly, but it can be useful if you need to enforce a login but you can’t expect a user to be able to log in interactively. For example, if you want to provide a restricted Atom feed, you can’t expect your users’ feed reader software to set your site’s login cookie by running through your HTML-based login process, but you can reasonably expect the software to understand HTTP basic authentication.

Unfortunately, if PHP is running under CGI — as is the default with PHP 5 on Dreamhost — then your PHP scripts typically cannot use HTTP authentication. Happily, there is a workaround; you can use mod_rewrite to pass HTTP authentication info to your scripts as a GET parameter.

This is adapted from these instructions by janhsh.

First, you need to set up an .htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /basedir/
    RewriteCond %{HTTP:Authorization} ^Basic.*
    RewriteRule (.*) index.php?Authorization=%{HTTP:Authorization} [QSA,L]
</IfModule>

In this example, HTTP auth info will be passed to the script index.php through a GET parameter called Authorization. The QSA bit means that any additional GET parameters will also be passed to your script.

Your PHP script could look something like this:

<?php
$authorized = false;

if (isset($_GET['Authorization'])) {
    // Check for the HTTP authentication string in $_GET['Authorization'], 
    // and put it in the $auth variable
    if (preg_match('/Basic\s+(.*)$/i', $_GET['Authorization'], $auth)) {
        // Split the string, base64 decode it, and place the values into 
        // the $authName and $authPassword variables
        list($authName, $authPassword) = explode(':', base64_decode($auth[1]));
        // Check the values of $authName and $authPass using your login routine
        if (do_some_sort_of_login_check($authName, $authPassword)) {
            $authorized = true;
        }
    }
}

if ($authorized) {
    // Success!  Display your content
    display_your_restricted_content();
} else {
    // Force the browser to prompt for a username and password
    header('WWW-Authenticate: Basic realm="name of your realm"');
    header('HTTP/1.0 401 Unauthorized');
    display_http_401_page();
}

[ article last updated 2007-02-27 22:04:09 by cobra libre ]