How to obtain the windows logon username using PHP

Source: Internet
Author: User
Tags chaine
This article mainly introduces how PHP obtains the windows login user name, which is implemented through NTLM. NTLM is a standard security protocol in earlier windows SNT versions, if you need a friend, you can refer to the question that was raised in the Q & A area a few days ago. all the friends who answered the question said it was impossible to use 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 ntlm get login * Version 0.2.1 * 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 th E 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 (); // obtain the user's header if (@ $ _ SERVER ['http _ VIA ']! = NULL) {// check whether a proxy is used, because ntlm authentication cannot pass through Proxy. echo "proxy bypass! ";} Elseif ($ headers ['authorization'] = NULL) {// si l 'entete autorisation est inexistante if the Authorization header does not exist ("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 '){ // Confirm 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 ($ retAu Th); // encode en base64 $ retAuth64 = trim ($ retAuth64); // enleve les espaces de debut et de fin header ("HTTP/1.0 401 Unauthorized "); // envoi le nouveau header ("WWW-Authenticate: NTLM $ retAuth64"); // avec l 'Identification suppl implements entaire exit ;} else if (ord ($ chained64 {8}) = 3) {// | _ byte signifiant l 'etape du processus d' identification (etape 5) // on recupere le domaine $ lenght_domai N = (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 ($ chained 64 [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! ";}}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.