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

2015-01-24信息快讯网

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

前几天在问答区提了一下这个问题,所有回答问题的朋友都说不可能通过PHP实现,碰巧我的实习负责人帮我找到了一个方法,貌似是通过NTLM来实现的,我是新手,对具体原理也知之不详,只是自己测试了一下,很好用.
所以赶快拿出来与大家分享.这是一个法国人写的,所以编码中的注释都是法语,如果有朋友很想了解某行的注释含义,请回帖说明,我可以试着翻译一下.

<?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){ // 确认是否使用了代理(proxy),因为ntlm验证不能穿过代理.
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 '){ // 确认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 supplmentaire
  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 donnes dans des variable de session
   $_SESSION['Login']=$login;
   header("Location: newpage.php");
   exit;
  }
  else{
   echo "NT Login empty!";
  }
   
 
  }
 }
}
?>

IIS下PHP的三种配置方式对比
PHP程序员常见的40个陋习,你中了几个?
php目录遍历函数opendir用法实例
php中base_convert()进制数字转换函数实例
PHP PDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别
PDO预处理语句PDOStatement对象使用总结
PHP中round()函数对浮点数进行四舍五入的方法
php管理nginx虚拟主机shell脚本实例
php中FTP函数ftp_connect、ftp_login与ftp_chmod用法
PHP遍历目录函数opendir()、readdir()、closedir()、rewinddir()总结
PHP移动文件指针ftell()、fseek()、rewind()函数总结
thinkphp视图模型查询提示ERR: 1146:Table 'db.pr_order_view' doesn't exist的解决方法
Windows下的PHP安装pear教程
Windows下安装PHP单元测试环境PHPUnit图文教程
JavaScript创建命名空间的5种写法
PHP获取MySql新增记录ID值的3种方法
PHP判断表单复选框选中状态完整例子
PHP网页游戏学习之Xnova(ogame)源码解读(十)
PHP网页游戏学习之Xnova(ogame)源码解读(九)
PHP网页游戏学习之Xnova(ogame)源码解读(八)
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
ThinkPHP结合ajax、Mysql实现的客户端通信功能代码示例
ThinkPHP实现事务回滚示例代码
PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法
ThinkPHP学习笔记(一)ThinkPHP部署
windows下配置apache+php+mysql时出现问题的处理方法
用PHP代替JS玩转DOM的思路及示例代码
使用PHP函数scandir排除特定目录
PHP数据库万能引擎类adodb配置使用以及实例集锦
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
教你如何在CI框架中使用 .htaccess 隐藏url中index.php
PHP解析html类库simple_html_dom的转码bug
PhpDocumentor 2安装以及生成API文档的方法
©2014-2024 dbsqp.com