How to obtain the Windows logon username using PHP)
| Font size subscription
I raised this question in the Q & A area a few days ago. All the friends who answered the question said it could not be implemented through PHP. It happened that my intern manager helped me find a way, it seems to be implemented through NTLM. I am a newbie and I do not know the specific principle. I just tested it myself and it is very useful.
So hurry up and share it with you. this is written by a French, so the comments in the Code are all in French. If a friend wants to understand the meaning of the comments in a line, please reply and explain it. I can try to translate it.
[PHP]
<? PHP
/*************************************** ********************************
**************************************** ********************************
*
* Php ntlm get Login
* Version 0.2.1
* Http://www.secusquad.com/ntlm/
* Copyright (c) 2004 Nicolas gollet (Nicolas (DOT) gollet (AT) secusquad (DOT) Com)
* Copyright (c) 2004 flextrow.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
* The Free Software Foundation; either version 2 of the license.
*
**************************************** *******************************/
Session_start ();
$ Headers = apache_request_headers (); // get the user Header
If (@ $ _ server ['HTTP _ via ']! = NULL) {// check whether a proxy is used, because NTLM authentication cannot pass through the proxy.
Echo "proxy bypass! ";
}
Elseif ($ headers ['authorization'] = NULL) {// Si L 'entete autorisation est inexistante if the license header does not exist
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'],) = 'ntlm') {// check whether the client is under NTLM
$ Chaine = $ headers ['authorization'];
$ Chaine = substr ($ chaine, 5); // get base64-encoded type1 Information
$ Chained64 = base64_decode ($ chaine); // decodes base64 to $ 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 (1, 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 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 ipvs dans des variable de session
$ _ Session ['login'] = $ login;
Header ("Location: Newpage. php ");
Exit;
}
Else {
Echo "nt login empty! ";
}
}
}
}
?>
[/PHP]