当前位置:首页 > PHP教程 > php应用 > 列表

PHP获取windows登录用户名的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-03-02 12:09:12 浏览: 评论:0 

这篇文章主要介绍了PHP获取windows登录用户名的方法,通过NTLM来实现,NTLM 是 Windows NT 早期版本的标准安全协议,需要的朋友可以参考下。

前几天在问答区提了一下这个问题,所有回答问题的朋友都说不可能通过PHP实现,碰巧我的实习负责人帮我找到了一个方法,貌似是通过NTLM来实现的,我是新手,对具体原理也知之不详,只是自己测试了一下,很好用.

所以赶快拿出来与大家分享.这是一个法国人写的,所以编码中的注释都是法语,如果有朋友很想了解某行的注释含义,请回帖说明,我可以试着翻译一下.

  1. <?php 
  2. /*********************************************************************** 
  3. ************************************************************************ 
  4. * 
  5. * PHP NTLM GET LOGIN  
  6. * Version 0.2.1                   
  7. * Copyright (c) 2004 Nicolas GOLLET ( Nicolas (dot) gollet (at) secusquad (dot) com ) 
  8. * Copyright (c) 2004 Flextronics Saint-Etienne 
  9. * 
  10. * This program is free software. You can redistribute it and/or modify  
  11. * it under the terms of the GNU General Public License as published by  
  12. * the Free Software Foundation; either version 2 of the License.     
  13. * 
  14. ***********************************************************************/ 
  15. session_start(); 
  16. $headers = apache_request_headers(); // 获取用户头 
  17. if (@$_SERVER['HTTP_VIA'] != NULL){ // 确认是否使用了代理(proxy),因为ntlm验证不能穿过代理. 
  18. echo "Proxy bypass!"; 
  19. } 
  20. elseif($headers['Authorization'] == NULL){  //si l'entete autorisation est inexistante如果许可头不存在 
  21.  header( "HTTP/1.0 401 Unauthorized" );  //envoi au client le mode d'identification 
  22.  header( "WWW-Authenticate: NTLM" );  //dans notre cas le NTLM 
  23.  exit;    //on quitte 
  24. } 
  25. if(isset($headers['Authorization']))   //dans le cas d'une authorisation (identification) 
  26. {  
  27.  if(substr($headers['Authorization'],0,5) == 'NTLM '){ // 确认client是否在ntlm下 
  28.  
  29.   $chaine=$headers['Authorization'];    
  30.   $chaine=substr($chaine, 5);  // 获取 base64-encoded type1 信息 
  31.   $chained64=base64_decode($chaine); // 解码 base64 到 $chained64 
  32.     
  33.   if(ord($chained64{8}) == 1){    
  34.   //   |_ byte signifiant l'etape du processus d'identification (etape 3)  
  35.    
  36.   // verification du drapeau NTLM "0xb2" ?l'offset 13 dans le message type-1-message (comp ie 5.5+) : 
  37.   if (ord($chained64[13]) != 178){ 
  38.    echo "NTLM Flag error!"; 
  39.    exit; 
  40.   } 
  41.  
  42.   $retAuth = "NTLMSSP".chr(000).chr(002).chr(000).chr(000).chr(000).chr(000).chr(000).chr(000); 
  43.   $retAuth .= chr(000).chr(040).chr(000).chr(000).chr(000).chr(001).chr(130).chr(000).chr(000); 
  44.   $retAuth .= chr(000).chr(002).chr(002).chr(002).chr(000).chr(000).chr(000).chr(000).chr(000); 
  45.   $retAuth .= chr(000).chr(000).chr(000).chr(000).chr(000).chr(000).chr(000); 
  46.     
  47.   $retAuth64 =base64_encode($retAuth); // encode en base64 
  48.   $retAuth64 = trim($retAuth64);  // enleve les espaces de debut et de fin 
  49.   header( "HTTP/1.0 401 Unauthorized" );  // envoi le nouveau header 
  50.   header( "WWW-Authenticate: NTLM $retAuth64" ); // avec l'identification suppl閙entaire 
  51.   exit; 
  52.     
  53.   } 
  54.     
  55.   else if(ord($chained64{8}) == 3){ 
  56.   //     |_ byte signifiant l'etape du processus d'identification (etape 5) 
  57.  
  58.   // on recupere le domaine 
  59.   $lenght_domain = (ord($chained64[31])*256 + ord($chained64[30])); // longueur du domain 
  60.   $offset_domain = (ord($chained64[33])*256 + ord($chained64[32])); // position du domain.  
  61.   $domain = str_replace("\0","",substr($chained64, $offset_domain, $lenght_domain)); // decoupage du du domain 
  62.     
  63.   //le login 
  64.   $lenght_login = (ord($chained64[39])*256 + ord($chained64[38])); // longueur du login. 
  65.   $offset_login = (ord($chained64[41])*256 + ord($chained64[40])); // position du login. 
  66.   $login = str_replace("\0","",substr($chained64, $offset_login, $lenght_login)); // decoupage du login 
  67.     
  68.   if ( $login != NULL){ 
  69.    // stockage des donn閑s dans des variable de session 
  70.    $_SESSION['Login']=$login; 
  71.    header("Location: newpage.php"); 
  72.    exit; 
  73.   } 
  74.   else{ 
  75.    echo "NT Login empty!"; 
  76.   } 
  77.      
  78.    
  79.   } 
  80.  } 
  81. } 
  82. ?> 

Tags: PHP获取windows登录用户名

分享到: