<?
/*
HTTP Basic Authentication using POP3
POP servers should be RFC 1939 Compliant and return
'+OK <whatever>' on success and '-ERR <whatever>' on failure.
YMMV of course.
I don't support this, don't ask me questions, yadda yadda
All this script does is authenticate - what you do from there
is up to you. I just really didn't want system password files
used for web authentication - which is a "Really Bad Idea".
Public Domain Source code - use as you see fit.
05-Oct-1998
joe@blarg.net
*/
$REALM = "My Realm";
$POPSERVER = 'pop3.yourdomain.com'; // Change this, please.
$LOGERRORS = 1; // Comment this line out to NOT log
// Authentication errors.
// Logs to STDERR - could use syslog
// with minor tweaking.
if(!isset($PHP_AUTH_USER)) {
Header("WWW-Authenticate: Basic realm=\"$REALM\"");
Header("HTTP/1.0 401 Unauthorized");
echo "<H1>Authorization Required</H1>\n";
exit;
} else {
$fp = fsockopen("$POPSERVER", 110, &$errno, &$errstr);
if(!$fp) {
if (isset($LOGERRORS)) {
error_log("AUTH ERROR ($PHP_AUTH_USER/$PHP_AUTH_PW) Connection Failure",0);
error_log("POP3 ERROR [$errno] [$errstr]",0);
}
Header("WWW-Authenticate: Basic realm=\"$REALM\"");
Header("HTTP/1.0 401 Auth Required");
echo "<H1>Authorization Required</H1>\n";
exit;
} else {
set_socket_blocking($fp,-1); // Turn off blocking
/*
Clear the POP server's Banner Text.
eg.. '+OK Welcome to <server name> etc etc'
*/
$trash = fgets($fp,128); // Trash to hold the banner
fwrite($fp,"USER $PHP_AUTH_USER\r\n"); // POP3 USER CMD
$user = fgets($fp,128);
$user = ereg_replace("\n","",$user);
if ( ereg ("^\+OK(.+)", $user ) ) {
fwrite($fp,"PASS $PHP_AUTH_PW\r\n"); // POP3 PASS CMD
$pass = fgets($fp,128);
$pass = ereg_replace("\n","",$pass);
if ( ereg ("^\+OK(.+)", $pass ) ) {
// User has successfully authenticated
echo "<BR>Authenticated: $pass<BR>\n";
if (isset($LOGERRORS)) {
error_log("AUTH OK: $PHP_AUTH_USER",0);
}
} else {
if (isset($LOGERRORS)) {
error_log("AUTH ERROR ($PHP_AUTH_USER/$PHP_AUTH_PW)",0);
error_log("POP3 ERROR $pass",0);
}
Header("WWW-Authenticate: Basic realm=\"$REALM\"");
Header("HTTP/1.0 401 Auth Required");
echo "<H1>Authorization Required</H1>\n";
exit;
}
} else {
if (isset($LOGERRORS)) {
error_log("AUTH ERROR ($PHP_AUTH_USER/$PHP_AUTH_PW)",0);
error_log("POP3 ERROR [$user]",0);
}
Header("WWW-Authenticate: Basic realm=\"$REALM\"");
Header("HTTP/1.0 401 Auth Required");
echo "<H1>Authorization Required</H1>\n";
exit;
}
fwrite($fp,"QUIT\r\n");
fclose($fp);
}
}
?>
redirect redirection ip address authentication authenticate addr Categories : Authentication , HTTP , Network , PHP Simple Password example Categories : PHP , Authentication , Security , HTTP Authentication HTTP protocol POST Categories : Authentication , HTTP , PHP A simple script to count and report hits and the last
modification time of an HTML page. Requires MySQL support
(other DBs should work too, except possibly mSQL). Categories : HTTP , MySQL , PHP , Databases Password using php, Javascript, and html form Categories : Security , PHP , Authentication , Java Script Protect your mailto: email addresses from bots - pure PHP Categories : PHP , Email , Security EAvalidator - This class can be used to validate an e-mail address by checking its domain. Categories : PHP , PHP Classes , Email , Regexps Solution to those 'tell-a-friend' type email issues Categories : PHP , Email , Databases , MySQL IP Blocking Categories : PHP , Security , HTTP phpEasyMail: An easy way to send data from HTML-forms via EMail. Categories : Email , HTML and PHP , Complete Programs , PHP header -- Send a raw HTTP header Categories : PHP , PHP Functions , HTTP How to let a user download a picture by clicking on it instead of needing to right click and Save-As. Categories : HTTP , PHP , HTML and PHP , Filesystem This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { } Categories : HTTP , Arrays , PHP Identify and log search engine access (spiders, robots, etc.) to a page. Categories : HTTP , Environment Variables , PHP , MySQL , Databases Complete, simple working example of login screen and check on a unique page using php functions, cookies and mysql database. Categories : PHP , Cookies , MySQL , HTML and PHP , Authentication
Paul Simpson wrote : 25
This code works a treat..
Thanks Ever so much
Kyle Geddes wrote : 30
Sweet Code! - Do you have any examples of how to
actually impliment this for a large site?
Tony Chamberlain wrote : 301
Just wanted to say I have been using this code for a
while now, works great! Highly recommended
Prachya Pantuyakorn wrote : 602
Hi, I`ve tested ur script. It`s very useful. Would u pls give me and idea to create mailbox by using php cript?
Best regard,
Prachya
Tal Eilon wrote : 629
Script works great! thanks a lot!!!!
--Tal