孤独海岸线 发表于 2017-4-13 12:34:39

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

  本案是为客户的合作单位开发的无线网络的网页认证系统。

本系统实现基于Web的Windows Active Directory用户验证(域用户验证)与数据库用户验证,调用Wiwiz Auth API实现Web认证。


无线网络为客户现有环境,每层一个无线路由器接到交换机,以前用WPA,现改为Web网页认证方式。

网络结构经小幅改动,Web认证网关采用Wiwiz虚拟机。

认证页面程序使用PHP + MySQL,调用Wiwiz Auth API接口。


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

MySQL数据库为客户的OA系统数据库。


有外网使用权限的员工可以使用公司的Windows域账户或者OA系统的账户登录并认证。

重要客户来访时接待人员还可以通过程序后台申请授权码。


认证页效果请参考下图(已隐去客户LOGO与名称):
http://img1.iyunv.com/attachment/201205/110633886.png
  以下附源码:
  index.php


[*]<?php
[*]//****************************************************
[*]//Getsincomingparameters
[*]//****************************************************
[*]
[*]$pTokencode=$_REQUEST["tokencode"];//incomingparameter"tokencode"
[*]$pSrvurl=$_REQUEST["srvurl"];//incomingparameter"srvurl"
[*]
[*]session_start();
[*]if($pTokencode!=null)
[*]$_SESSION['tokencode']=$pTokencode;
[*]if($pSrvurl!=null)
[*]$_SESSION['srvurl']=$pSrvurl;
[*]?>
[*]
[*]<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
[*]<html>
[*]<head>
[*]<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
[*]<metahttp-equiv="Content-Language"content="zh">
[*]<metahttp-equiv="Pragma"content="no-cache">
[*]<metahttp-equiv="Cache-Control"content="no-cache">
[*]
[*]<title>WifiPortal</title>
[*]<script>
[*]functiononLogin1(){
[*]
[*]if(document.getElementById("AuthType0").checked==false&&document.getElementById("AuthType1").checked==false){
[*]alert("请选择“使用OA系统账户登录”或“使用域账号登录”。");
[*]returnfalse;
[*]}
[*]
[*]if(document.getElementById("agree1").checked==false){
[*]alert("认证前您需要阅读并认同公司《计算机及网络使用管理规定》。");
[*]returnfalse;
[*]}
[*]
[*]returntrue;
[*]}
[*]
[*]functiononLogin2(){
[*]if(document.getElementById("agree2").checked==false){
[*]alert("认证前您需要阅读并认同《访客使用网络协议》。");
[*]returnfalse;
[*]}
[*]
[*]returntrue;
[*]}
[*]</script>
[*]
[*]</head>
[*]
[*]<body>
[*]<formaction="auth.php"method="post">
[*]
[*]<center>
[*]<br>
[*]<fontstyle="font-size:22px"color="red">
[*]<b>XXX公司WiFi认证系统</font>
[*]<br><br>
[*]
[*]<fontstyle="font-size:14px">
[*]本公司、子公司及合作单位员工请使用OA系统账户或域账户进行认证
[*]<br>
[*](仅限开通使用Internet权限的员工)
[*]<br><br>
[*]访客请使用授权码进行认证
[*]</font></b>
[*]
[*]<br>
[*]<br>
[*]
[*]<tablewidth="760">
[*]<tr>
[*]<tdwidth="60%">
[*]
[*]<tablewidth="90%"height="340"border="0"cellspacing="1"cellpadding="5"bgcolor="#cccccc"style="font-size:12px">
[*]<trbgcolor="#eef1ff">
[*]<tdalign=center>
[*]
[*]<b><fontstyle="font-size:14px">员工通道</b></font>
[*]
[*]
[*]<tablestyle="font-size:12px">
[*]<tr>
[*]<tdcolspan=2>
[*]
[*]<br>
[*]<inputtype="radio"name="AuthType"id="AuthType0"value="0"/><labelfor="AuthType0">使用OA系统账户登录</label>
[*]</td>
[*]</tr>
[*]
[*]<tr>
[*]<td>
[*]工号:
[*]</td>
[*]<td>
[*]<inputtype="text"name="StaffId"/>
[*]</td>
[*]</tr>
[*]
[*]<tr>
[*]<td>
[*]密码:
[*]</td>
[*]<td>
[*]<inputtype="password"name="Pswd"/>
[*]</td>
[*]</tr>
[*]
[*]<tr>
[*]<tdcolspan=2>
[*]<inputtype="radio"name="AuthType"id="AuthType1"value="1"/><labelfor="AuthType1">使用域账号登录</label>
[*]</td>
[*]</tr>
[*]
[*]<tr>
[*]<td>
[*]所在域:
[*]</td>
[*]<td>
[*]<selectname="Domain">
[*]<optionvalue="MD">MD</option>
[*]<optionvalue="Marketing">Marketing</option>
[*]</select>
[*]</td>
[*]</tr>
[*]
[*]<tr>
[*]<td>
[*]域账户名:
[*]</td>
[*]<td>
[*]<inputtype="text"name="DomainUser"/>
[*]</td>
[*]</tr>
[*]
[*]<tr>
[*]<td>
[*]密码:
[*]</td>
[*]<td>
[*]<inputtype="password"name="DomainPswd"/>
[*]</td>
[*]</tr>
[*]
[*]</table>
[*]
[*]<br>
[*]<inputtype="checkbox"name="agree1"id="agree1"><labelfor="agree1">我已阅读公司《<ahref="http://172.23.1.16/hr/docs/k/12_doc.htm">计算机及网络使用管理规定</a>》</label>
[*]<br><br>
[*]<inputtype="submit"value="登录/认证"name="login1"onclick="returnonLogin1();"/>
[*]
[*]</td>
[*]</tr>
[*]</table>
[*]
[*]</td>
[*]
[*]<tdwidth="40%">
[*]
[*]<tablewidth="90%"height="340"border="0"cellspacing="1"cellpadding="8"bgcolor="#cccccc"style="font-size:12px">
[*]<trbgcolor="#eef1ff">
[*]<td>
[*]<center>
[*]<b><fontstyle="font-size:14px">访客通道</b></font>
[*]<br><br>
[*]
[*]<tablestyle="font-size:12px">
[*]<tr>
[*]<tdcolspan=2>
[*]
[*]<center>访客请使用授权码进行认证<br><br>
[*]</td>
[*]</tr>
[*]
[*]<tr>
[*]<td>
[*]
[*]授权码:
[*]</td>
[*]<td>
[*]<inputtype="text"style="width:100px"name="AuthCode"/>
[*]
[*]</td>
[*]</tr>
[*]
[*]
[*]</td>
[*]</tr>
[*]</table>
[*]
[*]<br>
[*]<inputtype="checkbox"name="agree2"id="agree2"><labelfor="agree2">我已阅读并认同《<ahref="http://172.23.1.16/hr/docs/k/11_doc.htm">访客使用网络协议</a>》</label>
[*]<br><br>
[*]<inputtype="submit"value="认证"name="login2"onclick="returnonLogin2();"/>
[*]
[*]<br>
[*]<br>
[*]</center>
[*]*注:如您正在访问本公司并需要使用本公司WiFi网络,请联系您的接待人员或客户经理以索取授权码
[*]</td>
[*]</tr>
[*]</table>
[*]
[*]</td></tr></table>
[*]
[*]<br>
[*]<fontsize=-1>帮助热线:内线5220
[*]
[*]</center>
[*]</form>
[*]</body>
[*]</html>

  auth.php


[*]<?php
[*]session_start();
[*]
[*]$AD_HOST="XXXXXX:389";//ActiveDirectory服务器
[*]
[*]$userkey="XXXXXX";//WiwizUserKey
[*]
[*]$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")ordie("OA系统连接异常!");
[*]//$db=mysql_connect(":/tmp/mysql.sock","root")ordie("OA系统连接异常!");
[*]
[*]mysql_select_db("oadb",$db);
[*]$result=mysql_query("SELECTcount(staffid)ascntFROMusermasterwherestaffid='".mysql_real_escape_string($StaffId)."'andpswd='".mysql_real_escape_string($Pswd)."'",$db);
[*]
[*]$cnt=mysql_result($result,0,"cnt");
[*]
[*]mysql_close($db);
[*]
[*]if($cnt<>'0')
[*]$loginSuccess=true;
[*]
[*]}elseif($_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)ordie("连接失败!");
[*]
[*]if($conn){
[*]//
[*]ldap_set_option($conn,LDAP_OPT_PROTOCOL_VERSION,3);
[*]ldap_set_option($conn,LDAP_OPT_REFERRALS,0);//Bindingtoldapserver
[*]$bd=ldap_bind($conn,$DomainUser.'@'.$Domain,$DomainPswd);//ordie("域账户或密码错误。");
[*]if($bd)
[*]$loginSuccess=true;
[*]}else{
[*]echo"连接失败!";
[*]}
[*]}
[*]}
[*]
[*]if(isset($_POST['login2'])){//访客通道(使用授权码认证)
[*]$AuthCode=$_POST['AuthCode'];
[*]
[*]if($AuthCode=='')
[*]die("请输授权码!");
[*]
[*]$db=mysql_connect("localhost","root")ordie("OA系统连接异常!");
[*]
[*]mysql_select_db("oadb",$db);
[*]$result=mysql_query("SELECTcount(id)ascntFROMauthcodewherecode='".mysql_real_escape_string($AuthCode)."'",$db);
[*]
[*]$cnt=mysql_result($result,0,"cnt");
[*]
[*]mysql_close($db);
[*]
[*]if($cnt<>'0')
[*]$loginSuccess=true;
[*]}
[*]
[*]
[*]//
[*]//Dosomethingyouneed.
[*]//e.g.verifytheuser
[*]//......
[*]//
[*]
[*]if($loginSuccess==false){
[*]
[*]echo"认证失败!";//ifuserloginfailed,showanerrormessage
[*]
[*]}else{
[*]
[*]//****************************************************
[*]//Step2.Dothepre-authbycallingWiwizAuthAPI
[*]//IMPORTANT:Dothisonyourserverside(ASP,C#,JSP/Servlet,PHP...),
[*]//butDONOTdothisonyourclientside(HTML/Javascript)
[*]//****************************************************
[*]
[*]//parameter"action":REQUIRED!
[*]//setitto"1"toauthenticatetheuser
[*]//setitto"0"toblocktheuser
[*]$action="1";
[*]
[*]//parameter"tokencode":REQUIRED!
[*]//setidenticaltotheincomingparameter
[*]$tokencode=$_SESSION['tokencode'];
[*]
[*]//parameter"srvurl":REQUIRED!
[*]//setidenticaltotheincomingparameter
[*]$srvurl=$_SESSION['srvurl'];
[*]
[*]//parameter"endtime":OPTIONAL
[*]//Format:yyyy-mm-ddhh:MM:sse.g.2012-05-3121:39:00
[*]//setthisparametertosetthetimetoclosetheuser'sInternetconnection
[*]//Note:thevaluemustbeurl-encoded.
[*]//$endtime=urlencode('2012-05-3121:39:00');
[*]$endtime='';
[*]
[*]//parameter"postauth":OPTIONAL
[*]//E.g.http://www.YourDomain.com
[*]//setthisparametertoredirecttoaspecifiedURLafterauthenticated.
[*]//Note:thevalueshouldbeurl-encoded.
[*]//$postauth=urlencode("http://www.wiwiz.com");
[*]$postauth='';
[*]
[*]$parameters="?wiwiz_auth_api=1&ver=1.0".//parameter"wiwiz_auth_api"and"ver".Fixedvalue
[*]"&tokencode=".$tokencode.//parameter"tokencode".Seeabove
[*]"&userkey=".$userkey.//parameter"userkey".SetyourownUserKey
[*]"&action=".$action.//parameter"action".Seeabove
[*]"&endtime=".$endtime.//parameter"endtime".Seeabove
[*]"&postauth=".$postauth;//parameter"postauth".Seeabove
[*]
[*]$verifycode=file_get_contents($srvurl.$parameters);
[*]
[*]if(strpos($verifycode,"ERR")===0){
[*]//ifthereisanerror,showerrorcode
[*]echo"Error:".$verifycode;
[*]
[*]}else{
[*]//OK,now.doStep3.
[*]
[*]//****************************************************
[*]//Step3.CompletetheAuthenticationbycallingWiwizAuthAPI
[*]//****************************************************
[*]$redirectUrl=$srvurl.//usethevalueofincomingparameter"srvurl"astheredirectionaddress
[*]"?wiwiz_auth_api_login=1".//parameter"wiwiz_auth_api_login"
[*]"&tokencode=".$tokencode.//parameter"tokencode",setidenticaltotheincomingparameter
[*]"&verifycode=".$verifycode;//parameter"verifycode",setidenticaltotheincomingparameter
[*]ob_start();
[*]header("Location:".$redirectUrl);//finally,dotheredirection
[*]ob_flush();
[*]
[*]//echo"<script>location.href=\"".$redirectUrl."\"</script>";
[*]
[*]}
[*]
[*]}
[*]
[*]?>

  本文出自 “野兽技术博客” 博客,请务必保留此出处http://beastwu.blog.iyunv.com/5091229/865707
页: [1]
查看完整版本: 用PHP开发企业Wifi网络Web认证系统(附源码)