> php教程 > PHP源码 > 본문

Nginx 使用 PHP 进行 IMAP 的认证

PHP中文网
풀어 주다: 2016-05-25 17:15:10
원래의
1282명이 탐색했습니다.

首先假设一下条件:

Your Proxy server for pop/imap is running on 192.168.1.1

You have 2 backend pop/imap servers: 192.168.1.22 and 192.168.1.33

You have a webserver that you will use for the authentication and redirection logic 192.168.1.44.

The authentication script is /mail/auth.php

<?php

if (!isset($_SERVER["HTTP_AUTH_USER"] ) || !isset($_SERVER["HTTP_AUTH_PASS"] )){
fail();
}
$username=$_SERVER["HTTP_AUTH_USER"] ;
$userpass=$_SERVER["HTTP_AUTH_PASS"] ;
$protocol=$_SERVER["HTTP_AUTH_PROTOCOL"] ;
// default backend port
$backend_port=110;
if ($protocol=="imap") {
$backend_port=143;
}
if ($protocol=="smtp") {
$backend_port=25;
}
// nginx likes ip address so if your
// application gives back hostname, convert it to ip address here
$backend_ip["mailhost01"] ="192.168.1.22";
$backend_ip["mailhost02"] ="192.168.1.33";
// Authenticate the user or fail
if (!authuser($username,$userpass){
fail();
exit;
}
// Get the server for this user if we have reached so far
$userserver=getmailserver($username);
// Get the ip address of the server
// We are assuming that you backend returns hostname
// We try to get the ip else return what we got back
$server_ip=(isset($backend_ip[$userserver] )?$backend_ip[$userserver] :$userserver;
// Pass!
pass($server_ip, $backend_port);

//END


function authuser($user,$pass){
 // put your logic here to authen the user to any backend
 // you want (datbase, ldap, etc)
 // for example, we will just return true;
 return true;
}

function getmailserver($user){
 // put the logic here to get the mailserver
 // backend for the user. You can get this from
 // some database or ldap etc
 // dummy logic, all users that start with a,c,f and g get mailhost01
 // the others get mailhost02
 if in_array(substr($user,0,1), array("a","c","f","g")){
 return"mailhost01";
 } else {
 return"mailhost02";
}
}

function fail(){
 header("Auth-Status: Invalid login or password");
exit;
}

function pass($server,$port){
 header("Auth-Status: OK");
 header("Auth-Server: $server");
 header("Auth-Port: $port");
exit;
}
로그인 후 복사
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!