|
|
|
<?
/*
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);
}
}
?>
|
|
| Simple Password example Categories : PHP, Authentication, Security, HTTP | | | Authentication HTTP protocol POST Categories : Authentication, HTTP, PHP | | | redirect redirection ip address authentication authenticate addr Categories : Authentication, HTTP, Network, PHP | | | Basic Authentication with sessions Categories : PHP, Beginner Guides, Authentication, Form Processing, Sessions | | | Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate. Categories : Authentication, PHP | | | simple script to send emails via a html-form to different users Categories : Email, MySQL, PHP, Databases | | | The following snippet gives complete info about all submitted
HTTP_POST_VARS and HTTP_GET_VARS Categories : Variables, HTTP, PHP | | | Email a user with out exposing email address Categories : PHP, Databases, MySQL, Email | | | PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character. Categories : PHP, Algorithms, Security, Authentication, Encryption | | | 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 | | | header -- Send a raw HTTP header Categories : PHP, PHP Functions, HTTP | | | Broadcast HTML Email Categories : PHP, Email, MySQL, Databases | | | POP3 Class Categories : PHP Classes, PHP, Email | | | PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL | | | Function to remember password Categories : PHP, Authentication, Personalization and Membership | |
| | | | 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
| |
|
|
|