PHP獲取windows登錄用戶名的方法
前幾天在問答區(qū)提了一下這個(gè)問題,所有回答問題的朋友都說不可能通過PHP實(shí)現(xiàn),碰巧我的實(shí)習(xí)負(fù)責(zé)人幫我找到了一個(gè)方法,貌似是通過NTLM來實(shí)現(xiàn)的,我是新手,對具體原理也知之不詳,只是自己測試了一下,很好用.
所以趕快拿出來與大家分享.這是一個(gè)法國人寫的,所以編碼中的注釋都是法語,如果有朋友很想了解某行的注釋含義,請回帖說明,我可以試著翻譯一下.
<?php
/***********************************************************************
************************************************************************
*
* PHP NTLM GET LOGIN
* Version 0.2.1
* Copyright (c) 2004 Nicolas GOLLET ( Nicolas (dot) gollet (at) secusquad (dot) com )
* Copyright (c) 2004 Flextronics Saint-Etienne
*
* This program is free software. You can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
***********************************************************************/
session_start();
$headers = apache_request_headers(); // 獲取用戶頭
if (@$_SERVER['HTTP_VIA'] != NULL){ // 確認(rèn)是否使用了代理(proxy),因?yàn)閚tlm驗(yàn)證不能穿過代理.
echo "Proxy bypass!";
}
elseif($headers['Authorization'] == NULL){ //si l'entete autorisation est inexistante如果許可頭不存在
header( "HTTP/1.0 401 Unauthorized" ); //envoi au client le mode d'identification
header( "WWW-Authenticate: NTLM" ); //dans notre cas le NTLM
exit; //on quitte
}
if(isset($headers['Authorization'])) //dans le cas d'une authorisation (identification)
{
if(substr($headers['Authorization'],0,5) == 'NTLM '){ // 確認(rèn)client是否在ntlm下
$chaine=$headers['Authorization'];
$chaine=substr($chaine, 5); // 獲取 base64-encoded type1 信息
$chained64=base64_decode($chaine); // 解碼 base64 到 $chained64
if(ord($chained64{8}) == 1){
// |_ byte signifiant l'etape du processus d'identification (etape 3)
// verification du drapeau NTLM "0xb2" ?l'offset 13 dans le message type-1-message (comp ie 5.5+) :
if (ord($chained64[13]) != 178){
echo "NTLM Flag error!";
exit;
}
$retAuth = "NTLMSSP".chr(000).chr(002).chr(000).chr(000).chr(000).chr(000).chr(000).chr(000);
$retAuth .= chr(000).chr(040).chr(000).chr(000).chr(000).chr(001).chr(130).chr(000).chr(000);
$retAuth .= chr(000).chr(002).chr(002).chr(002).chr(000).chr(000).chr(000).chr(000).chr(000);
$retAuth .= chr(000).chr(000).chr(000).chr(000).chr(000).chr(000).chr(000);
$retAuth64 =base64_encode($retAuth); // encode en base64
$retAuth64 = trim($retAuth64); // enleve les espaces de debut et de fin
header( "HTTP/1.0 401 Unauthorized" ); // envoi le nouveau header
header( "WWW-Authenticate: NTLM $retAuth64" ); // avec l'identification suppl閙entaire
exit;
}
else if(ord($chained64{8}) == 3){
// |_ byte signifiant l'etape du processus d'identification (etape 5)
// on recupere le domaine
$lenght_domain = (ord($chained64[31])*256 + ord($chained64[30])); // longueur du domain
$offset_domain = (ord($chained64[33])*256 + ord($chained64[32])); // position du domain.
$domain = str_replace("\0","",substr($chained64, $offset_domain, $lenght_domain)); // decoupage du du domain
//le login
$lenght_login = (ord($chained64[39])*256 + ord($chained64[38])); // longueur du login.
$offset_login = (ord($chained64[41])*256 + ord($chained64[40])); // position du login.
$login = str_replace("\0","",substr($chained64, $offset_login, $lenght_login)); // decoupage du login
if ( $login != NULL){
// stockage des donn閑s dans des variable de session
$_SESSION['Login']=$login;
header("Location: newpage.php");
exit;
}
else{
echo "NT Login empty!";
}
}
}
}
?>
- PHP+Ajax驗(yàn)證碼驗(yàn)證用戶登錄
- php有效防止同一用戶多次登錄
- WAMPserver配置方法(允許外部訪問、phpmyadmin設(shè)置為輸入用戶名密碼才可登錄等)
- php中使用session防止用戶非法登錄后臺的方法
- ThinkPHP之用戶注冊登錄留言完整實(shí)例
- php中如何同時(shí)使用session和cookie來保存用戶登錄信息
- php中使用cookie來保存用戶登錄信息的實(shí)現(xiàn)代碼
- php下獲取Discuz論壇登錄用戶名、用戶組、用戶ID等信息的實(shí)現(xiàn)代碼
- PHP判斷用戶是否已經(jīng)登錄(跳轉(zhuǎn)到不同頁面或者執(zhí)行不同動作)
相關(guān)文章
php強(qiáng)制用戶轉(zhuǎn)向www域名的方法
這篇文章主要介紹了php強(qiáng)制用戶轉(zhuǎn)向www域名的方法,可實(shí)現(xiàn)模擬301重定向的功能,并且針對無法head重定向的情況輸出鏈接,需要的朋友可以參考下2015-06-06
php生成百度sitemap站點(diǎn)地圖類函數(shù)實(shí)例
這篇文章主要介紹了php生成百度sitemap站點(diǎn)地圖類函數(shù)的方法,詳細(xì)講述了百度站點(diǎn)sitemap的實(shí)現(xiàn)方法與注意事項(xiàng),在web站點(diǎn)的建設(shè)中非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
php實(shí)現(xiàn)在多維數(shù)組中查找特定value的方法
這篇文章主要介紹了php實(shí)現(xiàn)在多維數(shù)組中查找特定value的方法,實(shí)例分析了php實(shí)現(xiàn)多維數(shù)組的遍歷及unset刪除的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
PHPStorm+XDebug進(jìn)行調(diào)試圖文教程
這篇文章主要為大家詳細(xì)介紹了PHPStorm+XDebug進(jìn)行調(diào)試圖文教程,內(nèi)容很豐富,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
PHP基于工廠模式實(shí)現(xiàn)的計(jì)算器實(shí)例
這篇文章主要介紹了PHP基于工廠模式實(shí)現(xiàn)的計(jì)算器,實(shí)例分析了php工廠模式的實(shí)現(xiàn)原理與應(yīng)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

