当前位置: 首页 > news >正文

用PHP开发企业Wifi网络Web认证系统(附源码)

文字转载自https://blog.51cto.com/beastwu/865707

本案是为客户的合作单位开发的无线网络的网页认证系统。
本系统实现基于Web的Windows Active Directory用户验证(域用户验证)与数据库用户验证,调用Wiwiz Auth API实现Web认证。

无线网络为客户现有环境,每层一个无线路由器接到交换机,以前用WPA,现改为Web网页认证方式。
网络结构经小幅改动,Web认证网关采用Wiwiz虚拟机。
认证页面程序使用PHP + MySQL,调用Wiwiz Auth API接口。

该公司有数百员工,网络使用方面规定较严格。多数员工只能上内网,少部分员工有使用外网的权限(客户OA系统中登记备案)。
MySQL数据库为客户的OA系统数据库。

有外网使用权限的员工可以使用公司的Windows域账户或者OA系统的账户登录并认证。
重要客户来访时接待人员还可以通过程序后台申请授权码。

认证页效果请参考下图(已隐去客户LOGO与名称):

以下附源码:

index.php

<?php 
//**************************************************** 
// Gets incoming parameters 
//**************************************************** 

$pTokencode = $_REQUEST["tokencode"];   // incoming parameter "tokencode" 
$pSrvurl = $_REQUEST["srvurl"];     // incoming parameter "srvurl" 

session_start(); 
if($pTokencode != null)  
    $_SESSION['tokencode'] = $pTokencode; 
if($pSrvurl != null) 
    $_SESSION['srvurl'] = $pSrvurl; 
?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<meta http-equiv="Content-Language" content="zh"> 
<meta http-equiv="Pragma" content="no-cache"> 
<meta http-equiv="Cache-Control" content="no-cache"> 

<title>Wifi Portal</title> 
<script> 
function onLogin1() { 

    if(document.getElementById("AuthType0").checked == false && document.getElementById("AuthType1").checked == false) { 
        alert("请选择“使用OA系统账户登录”或“使用域账号登录”。"); 
        return false;        
    } 

    if(document.getElementById("agree1").checked == false) { 
        alert("认证前您需要阅读并认同公司《计算机及网络使用管理规定》。"); 
        return false; 
    } 

    return true; 
} 

function onLogin2() { 
    if(document.getElementById("agree2").checked == false) { 
        alert("认证前您需要阅读并认同《访客使用网络协议》。"); 
        return false; 
    } 

    return true; 
} 
</script> 

</head> 

<body> 
<form action="auth.php" method="post"> 

<center> 
<br> 
<font style="font-size:22px" color="red"> 
<b>XXX公司WiFi认证系统</font> 
<br><br> 

<font style="font-size:14px"> 
本公司、子公司及合作单位员工请使用OA系统账户或域账户进行认证 
<br> 
(仅限开通使用Internet权限的员工) 
<br><br> 
访客请使用授权码进行认证 
</font></b> 

<br> 
<br> 

<table width="760"> 
<tr> 
<td width="60%"> 

<table width="90%" height="340" border="0" cellspacing="1" cellpadding="5" bgcolor="#cccccc" style="font-size: 12px"> 
<tr bgcolor="#eef1ff"> 
<td align=center> 

<b><font style="font-size:14px">员工通道</b></font> 

<table style="font-size:12px"> 
<tr> 
<td colspan=2> 

<br> 
<input type="radio" name="AuthType" id="AuthType0" value="0" /> <label for="AuthType0">使用OA系统账户登录</label> 
</td> 
</tr> 

<tr> 
<td> 
工号: 
</td> 
<td> 
<input type="text" name="StaffId" /> 
</td> 
</tr> 

<tr> 
<td> 
密码: 
</td> 
<td> 
<input type="password" name="Pswd" /> 
</td> 
</tr> 

<tr> 
<td colspan=2> 
<input type="radio" name="AuthType" id="AuthType1" value="1" /> <label for="AuthType1">使用域账号登录</label> 
</td> 
</tr> 

<tr> 
<td> 
所在域: 
</td> 
<td> 
<select name="Domain"> 
<option value="MD">MD</option> 
<option value="Marketing">Marketing</option> 
</select> 
</td> 
</tr> 

<tr> 
<td> 
域账户名: 
</td> 
<td> 
<input type="text" name="DomainUser" /> 
</td> 
</tr> 

<tr> 
<td> 
密码: 
</td> 
<td> 
<input type="password" name="DomainPswd" /> 
</td> 
</tr> 

</table> 

<br> 
<input type="checkbox" name="agree1" id="agree1"><label for="agree1">我已阅读公司《<a href="http://172.23.1.16/hr/docs/k/12_doc.htm">计算机及网络使用管理规定</a>》</label> 
<br><br> 
<input type="submit" value="  登录 / 认证  " name="login1" onclick="return onLogin1();" /> 

</td> 
</tr> 
</table> 

</td> 

<td width="40%"> 

<table width="90%" height="340" border="0" cellspacing="1" cellpadding="8" bgcolor="#cccccc" style="font-size: 12px"> 
<tr bgcolor="#eef1ff"> 
<td> 
<center> 
<b><font style="font-size:14px">访客通道</b></font> 
<br><br> 

<table style="font-size:12px"> 
<tr> 
<td colspan=2> 

<center> 访客请使用授权码进行认证 <br><br> 
</td> 
</tr> 

<tr> 
<td> 

授权码: 
</td> 
<td> 
<input type="text" style="width:100px" name="AuthCode" /> 

</td> 
</tr> 

</td> 
</tr> 
</table> 

<br> 
<input type="checkbox" name="agree2" id="agree2"><label for="agree2">我已阅读并认同《<a href="http://172.23.1.16/hr/docs/k/11_doc.htm">访客使用网络协议</a>》</label> 
<br><br> 
<input type="submit" value="  认证  " name="login2" onclick="return onLogin2();" /> 

<br> 
<br> 
</center> 
*注: 如您正在访问本公司并需要使用本公司WiFi网络,请联系您的接待人员或客户经理以索取授权码 
</td> 
</tr> 
</table> 

</td></tr></table> 

<br> 
<font size=-1>帮助热线:内线5220    

</center> 
</form> 
</body> 
</html> 

auth.php

<?php 
session_start(); 

$AD_HOST = "XXXXXX:389";    // Active Directory服务器 

$userkey = "XXXXXX";    // Wiwiz User Key 

$loginSuccess = false; 

if(isset($_POST['login1'])) { 
    if($_POST['AuthType'] == '0' ) {            //员工通道 - 使用OA系统账户登录并认证 
        $StaffId = $_POST['StaffId']; 
        $Pswd = $_POST['Pswd']; 

        if($StaffId == '') 
            die("请输入工号!" ); 
        if($Pswd == '') 
            die("请输入密码!" ); 

        $db = mysql_connect("localhost", "root") or die("OA系统连接异常!"); 
        //$db = mysql_connect(":/tmp/mysql.sock", "root") or die("OA系统连接异常!");       

        mysql_select_db("oadb",$db); 
        $result = mysql_query("SELECT count(staffid) as cnt FROM usermaster where staffid='". mysql_real_escape_string($StaffId) ."' and pswd='". mysql_real_escape_string($Pswd) ."'", $db); 

        $cnt = mysql_result($result, 0, "cnt"); 

        mysql_close($db); 

        if($cnt <> '0') 
            $loginSuccess = true; 

    } else if($_POST['AuthType'] == '1' ) { //员工通道 - 使用域账户登录并认证 
        $DomainUser = $_POST['DomainUser']; 
        $DomainPswd = $_POST['DomainPswd']; 
        $Domain = $_POST['Domain']; 

        if($DomainUser == '') 
            die("请输入域账户名!" ); 
        if($DomainPswd == '') 
            die("请输入域账户密码!" ); 
        if($Domain == '') 
            die("请选择所在域!" ); 

        $conn = ldap_connect($AD_HOST) or die("连接失败!" ); 

        if($conn){ 
            // 
            ldap_set_option ( $conn, LDAP_OPT_PROTOCOL_VERSION, 3 ); 
            ldap_set_option ( $conn, LDAP_OPT_REFERRALS, 0 ); // Binding to ldap server 
            $bd = ldap_bind($conn, $DomainUser.'@'.$Domain, $DomainPswd); //  or die ("域账户或密码错误。"); 
            if($bd) 
                $loginSuccess = true; 
        } else{ 
            echo "连接失败!"; 
        } 
    } 
} 

if(isset($_POST['login2'])) {               //访客通道(使用授权码认证) 
    $AuthCode = $_POST['AuthCode']; 

    if($AuthCode == '') 
        die("请输授权码!" ); 

    $db = mysql_connect("localhost", "root") or die("OA系统连接异常!"); 

    mysql_select_db("oadb",$db); 
    $result = mysql_query("SELECT count(id) as cnt FROM authcode where code='". mysql_real_escape_string($AuthCode). "'", $db); 

    $cnt = mysql_result($result, 0, "cnt"); 

    mysql_close($db); 

    if($cnt <> '0') 
        $loginSuccess = true; 
} 

// 
// Do something you need. 
// e.g. verify the user 
//      ...... 
// 

if($loginSuccess == false) { 

    echo "认证失败!";   // if user login failed, show an error message 

} else { 

    //**************************************************** 
    // Step 2. Do the pre-auth by calling Wiwiz Auth API 
    // IMPORTANT: Do this on your server side(ASP, C#, JSP/Servlet, PHP...),  
    //            but DO NOT do this on your client side (HTML/Javascript) 
    //**************************************************** 

    // parameter "action" : REQUIRED! 
    // set it to "1" to authenticate the user 
    // set it to "0" to block the user 
    $action = "1"; 

    // parameter "tokencode": REQUIRED! 
    // set identical to the incoming parameter 
    $tokencode = $_SESSION['tokencode']; 

    // parameter "srvurl": REQUIRED! 
    // set identical to the incoming parameter   
    $srvurl = $_SESSION['srvurl']; 

    // parameter "endtime" : OPTIONAL 
    // Format: yyyy-mm-dd hh:MM:ss  e.g. 2012-05-31 21:39:00 
    // set this parameter to set the time to close the user's Internet connection  
    // Note: the value must be url-encoded.   
//  $endtime = urlencode('2012-05-31 21:39:00'); 
    $endtime = '';   

    // parameter "postauth" : OPTIONAL 
    // E.g. http://www.YourDomain.com 
    // set this parameter to redirect to a specified URL after authenticated. 
    // Note: the value should be url-encoded.   
    //$postauth = urlencode("http://www.wiwiz.com"); 
    $postauth = '';  

    $parameters = "?wiwiz_auth_api=1&ver=1.0". // parameter "wiwiz_auth_api" and "ver". Fixed value 
            "&tokencode=". $tokencode . // parameter "tokencode". See above 
            "&userkey=". $userkey .     // parameter "userkey". Set your own User Key 
            "&action=". $action .       // parameter "action". See above 
            "&endtime=". $endtime .     // parameter "endtime". See above 
            "&postauth=". $postauth;    // parameter "postauth". See above 

    $verifycode = file_get_contents($srvurl . $parameters); 

    if (strpos ($verifycode, "ERR") === 0) { 
        // if there is an error, show error code 
        echo "Error: ". $verifycode; 

    } else { 
        // OK, now. do Step 3. 

        //**************************************************** 
        // Step 3. Complete the Authentication by calling Wiwiz Auth API 
        //****************************************************   
        $redirectUrl = $srvurl.     // use the value of incoming parameter "srvurl" as the redirection address 
                "?wiwiz_auth_api_login=1".  // parameter "wiwiz_auth_api_login" 
                "&tokencode=". $tokencode . // parameter "tokencode", set identical to the incoming parameter    
                "&verifycode=". $verifycode;    // parameter "verifycode", set identical to the incoming parameter   
        ob_start(); 
        header("Location: ". $redirectUrl); // finally, do the redirection 
        ob_flush(); 

//          echo "<script>location.href=\"". $redirectUrl ."\"</script>"; 

    } 

} 

?> 

附件:http://down.51cto.com/data/2360589

转载于:https://blog.51cto.com/aiphp/2090243

相关文章:

  • ansible主机清单Inventory说明
  • JMeter正则表达式提取器
  • Java SPI(Service Provider Interface)简介
  • Git 企业中常用分支管理策略
  • Linux的overcommit配置
  • 比特币现金改变了我们的生活
  • 将用户当前表空间中的表和索引迁移到另一个表空间
  • Apache OFBIZ高速上手(三)--文件夹amp;amp;配置文件介绍
  • 字符串输入
  • 06.C(指针)
  • JavaEE GenericServlet 解析
  • 算法系列15天速成——第十四天 图【上】
  • ionic3 关于屏幕方向问题
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • Confluence 6 更新目录
  • [nginx文档翻译系列] 控制nginx
  •  D - 粉碎叛乱F - 其他起义
  • Docker下部署自己的LNMP工作环境
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • isset在php5.6-和php7.0+的一些差异
  • PAT A1092
  • use Google search engine
  • ViewService——一种保证客户端与服务端同步的方法
  • Vim 折腾记
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 代理模式
  • 高程读书笔记 第六章 面向对象程序设计
  • 老板让我十分钟上手nx-admin
  • 前端设计模式
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 设计模式(12)迭代器模式(讲解+应用)
  • 事件委托的小应用
  • 算法系列——算法入门之递归分而治之思想的实现
  • 一天一个设计模式之JS实现——适配器模式
  • 最简单的无缝轮播
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • ​什么是bug?bug的源头在哪里?
  • # Apache SeaTunnel 究竟是什么?
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • $.ajax,axios,fetch三种ajax请求的区别
  • (11)MATLAB PCA+SVM 人脸识别
  • (DFS + 剪枝)【洛谷P1731】 [NOI1999] 生日蛋糕
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (三)elasticsearch 源码之启动流程分析
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .NET中两种OCR方式对比
  • .xml 下拉列表_RecyclerView嵌套recyclerview实现二级下拉列表,包含自定义IOS对话框...
  • /etc/fstab 只读无法修改的解决办法
  • @Bean有哪些属性
  • @ResponseBody