<轉>用PHP開發企業Wifi網絡Web認證系統(附源碼)

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://beastwu.blog.51cto.com/5091229/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

 

  1. <?php 
  2. //**************************************************** 
  3. // Gets incoming parameters 
  4. //**************************************************** 
  5.   
  6. $pTokencode = $_REQUEST["tokencode"];   // incoming parameter "tokencode" 
  7. $pSrvurl = $_REQUEST["srvurl"];     // incoming parameter "srvurl" 
  8.   
  9. session_start(); 
  10. if($pTokencode != null)  
  11.     $_SESSION['tokencode'] = $pTokencode
  12. if($pSrvurl != null) 
  13.     $_SESSION['srvurl'] = $pSrvurl
  14. ?> 
  15.   
  16. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
  17. <html> 
  18. <head> 
  19. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  20. <meta http-equiv="Content-Language" content="zh"
  21. <meta http-equiv="Pragma" content="no-cache"
  22. <meta http-equiv="Cache-Control" content="no-cache"
  23.  
  24. <title>Wifi Portal</title> 
  25. <script> 
  26. function onLogin1() { 
  27.      
  28.     if(document.getElementById("AuthType0").checked == false && document.getElementById("AuthType1").checked == false) { 
  29.         alert("請選擇“使用OA系統賬戶登錄”或“使用域賬號登錄”。"); 
  30.         return false;        
  31.     } 
  32.          
  33.     if(document.getElementById("agree1").checked == false) { 
  34.         alert("認證前您需要閱讀並認同公司《計算機及網絡使用管理規定》。"); 
  35.         return false; 
  36.     } 
  37.      
  38.     return true; 
  39.  
  40. function onLogin2() { 
  41.     if(document.getElementById("agree2").checked == false) { 
  42.         alert("認證前您需要閱讀並認同《訪客使用網絡協議》。"); 
  43.         return false; 
  44.     } 
  45.      
  46.     return true; 
  47. </script> 
  48.  
  49. </head> 
  50.  
  51. <body> 
  52. <form action="auth.php" method="post"
  53.  
  54. <center> 
  55. <br> 
  56. <font style="font-size:22px" color="red"
  57. <b>XXX公司WiFi認證系統</font> 
  58. <br><br> 
  59.  
  60. <font style="font-size:14px"
  61. 本公司、子公司及合作單位員工請使用OA系統賬戶或域賬戶進行認證 
  62. <br> 
  63. (僅限開通使用Internet權限的員工) 
  64. <br><br> 
  65. 訪客請使用授權碼進行認證 
  66. </font></b> 
  67.  
  68. <br> 
  69. <br> 
  70.  
  71. <table width="760"
  72. <tr> 
  73. <td width="60%"
  74.  
  75. <table width="90%" height="340" border="0" cellspacing="1" cellpadding="5" bgcolor="#cccccc" style="font-size: 12px"
  76. <tr bgcolor="#eef1ff"
  77. <td align=center> 
  78.  
  79. <b><font style="font-size:14px">員工通道</b></font> 
  80.  
  81.  
  82. <table style="font-size:12px"
  83. <tr> 
  84. <td colspan=2> 
  85.  
  86. <br> 
  87. <input type="radio" name="AuthType" id="AuthType0" value="0" /> <label for="AuthType0">使用OA系統賬戶登錄</label> 
  88. </td> 
  89. </tr> 
  90.  
  91. <tr> 
  92. <td> 
  93. 工號: 
  94. </td> 
  95. <td> 
  96. <input type="text" name="StaffId" /> 
  97. </td> 
  98. </tr> 
  99.  
  100. <tr> 
  101. <td> 
  102. 密碼: 
  103. </td> 
  104. <td> 
  105. <input type="password" name="Pswd" /> 
  106. </td> 
  107. </tr> 
  108.  
  109. <tr> 
  110. <td colspan=2> 
  111. <input type="radio" name="AuthType" id="AuthType1" value="1" /> <label for="AuthType1">使用域賬號登錄</label> 
  112. </td> 
  113. </tr> 
  114.  
  115. <tr> 
  116. <td> 
  117. 所在域: 
  118. </td> 
  119. <td> 
  120. <select name="Domain"
  121. <option value="MD">MD</option> 
  122. <option value="Marketing">Marketing</option> 
  123. </select> 
  124. </td> 
  125. </tr> 
  126.  
  127. <tr> 
  128. <td> 
  129. 域賬戶名: 
  130. </td> 
  131. <td> 
  132. <input type="text" name="DomainUser" /> 
  133. </td> 
  134. </tr> 
  135.  
  136. <tr> 
  137. <td> 
  138. 密碼: 
  139. </td> 
  140. <td> 
  141. <input type="password" name="DomainPswd" /> 
  142. </td> 
  143. </tr> 
  144.  
  145. </table> 
  146.  
  147. <br> 
  148. <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> 
  149. <br><br> 
  150. <input type="submit" value="  登錄 / 認證  " name="login1" onclick="return onLogin1();" /> 
  151.  
  152. </td> 
  153. </tr> 
  154. </table> 
  155.  
  156. </td> 
  157.  
  158. <td width="40%"
  159.  
  160. <table width="90%" height="340" border="0" cellspacing="1" cellpadding="8" bgcolor="#cccccc" style="font-size: 12px"
  161. <tr bgcolor="#eef1ff"
  162. <td> 
  163. <center> 
  164. <b><font style="font-size:14px">訪客通道</b></font> 
  165. <br><br> 
  166.  
  167. <table style="font-size:12px"
  168. <tr> 
  169. <td colspan=2> 
  170.  
  171. <center> 訪客請使用授權碼進行認證 <br><br> 
  172. </td> 
  173. </tr> 
  174.  
  175. <tr> 
  176. <td> 
  177.  
  178. 授權碼: 
  179. </td> 
  180. <td> 
  181. <input type="text" style="width:100px" name="AuthCode" /> 
  182.  
  183. </td> 
  184. </tr> 
  185.  
  186.  
  187. </td> 
  188. </tr> 
  189. </table> 
  190.  
  191. <br> 
  192. <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> 
  193. <br><br> 
  194. <input type="submit" value="  認證  " name="login2" onclick="return onLogin2();" /> 
  195.  
  196. <br> 
  197. <br> 
  198. </center> 
  199. *注: 如您正在訪問本公司並需要使用本公司WiFi網絡,請聯繫您的接待人員或客戶經理以索取授權碼 
  200. </td> 
  201. </tr> 
  202. </table> 
  203.  
  204. </td></tr></table> 
  205.  
  206. <br> 
  207. <font size=-1>幫助熱線:內線5220    
  208.  
  209. </center> 
  210. </form> 
  211. </body> 
  212. </html> 

auth.php

 

  1. <?php 
  2. session_start(); 
  3.  
  4. $AD_HOST = "XXXXXX:389";    // Active Directory服務器 
  5.      
  6. $userkey = "XXXXXX";    // Wiwiz User Key 
  7.  
  8. $loginSuccess = false; 
  9.  
  10. if(isset($_POST['login1'])) { 
  11.     if($_POST['AuthType'] == '0' ) {            //員工通道 - 使用OA系統賬戶登錄並認證 
  12.         $StaffId = $_POST['StaffId']; 
  13.         $Pswd = $_POST['Pswd']; 
  14.          
  15.         if($StaffId == ''
  16.             die("請輸入工號!" ); 
  17.         if($Pswd == ''
  18.             die("請輸入密碼!" ); 
  19.          
  20.         $db = mysql_connect("localhost""root"or die("OA系統連接異常!"); 
  21.         //$db = mysql_connect(":/tmp/mysql.sock", "root") or die("OA系統連接異常!");       
  22.          
  23.         mysql_select_db("oadb",$db); 
  24.         $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); 
  25.          
  26.         $cnt = mysql_result($result, 0, "cnt"); 
  27.          
  28.         mysql_close($db); 
  29.          
  30.         if($cnt <> '0'
  31.             $loginSuccess = true; 
  32.          
  33.     } else if($_POST['AuthType'] == '1' ) { //員工通道 - 使用域賬戶登錄並認證 
  34.         $DomainUser = $_POST['DomainUser']; 
  35.         $DomainPswd = $_POST['DomainPswd']; 
  36.         $Domain = $_POST['Domain']; 
  37.  
  38.         if($DomainUser == ''
  39.             die("請輸入域賬戶名!" ); 
  40.         if($DomainPswd == ''
  41.             die("請輸入域賬戶密碼!" ); 
  42.         if($Domain == ''
  43.             die("請選擇所在域!" ); 
  44.          
  45.         $conn = ldap_connect($AD_HOSTor die("連接失敗!" ); 
  46.  
  47.         if($conn){ 
  48.             // 
  49.             ldap_set_option ( $conn, LDAP_OPT_PROTOCOL_VERSION, 3 ); 
  50.             ldap_set_option ( $conn, LDAP_OPT_REFERRALS, 0 ); // Binding to ldap server 
  51.             $bd = ldap_bind($conn$DomainUser.'@'.$Domain$DomainPswd); //  or die ("域賬戶或密碼錯誤。"); 
  52.             if($bd
  53.                 $loginSuccess = true; 
  54.         } else
  55.             echo "連接失敗!"
  56.         } 
  57.     } 
  58.  
  59. if(isset($_POST['login2'])) {               //訪客通道(使用授權碼認證) 
  60.     $AuthCode = $_POST['AuthCode']; 
  61.      
  62.     if($AuthCode == ''
  63.         die("請輸授權碼!" ); 
  64.      
  65.     $db = mysql_connect("localhost""root"or die("OA系統連接異常!"); 
  66.      
  67.     mysql_select_db("oadb",$db); 
  68.     $result = mysql_query("SELECT count(id) as cnt FROM authcode where code='". mysql_real_escape_string($AuthCode). "'"$db); 
  69.      
  70.     $cnt = mysql_result($result, 0, "cnt"); 
  71.      
  72.     mysql_close($db); 
  73.      
  74.     if($cnt <> '0'
  75.         $loginSuccess = true; 
  76.   
  77.  
  78. // 
  79. // Do something you need. 
  80. // e.g. verify the user 
  81. //      ...... 
  82. // 
  83.  
  84. if($loginSuccess == false) { 
  85.  
  86.     echo "認證失敗!";   // if user login failed, show an error message 
  87.  
  88. else { 
  89.  
  90.     //**************************************************** 
  91.     // Step 2. Do the pre-auth by calling Wiwiz Auth API 
  92.     // IMPORTANT: Do this on your server side(ASP, C#, JSP/Servlet, PHP...),  
  93.     //            but DO NOT do this on your client side (HTML/Javascript) 
  94.     //**************************************************** 
  95.  
  96.     // parameter "action" : REQUIRED! 
  97.     // set it to "1" to authenticate the user 
  98.     // set it to "0" to block the user 
  99.     $action = "1"
  100.  
  101.     // parameter "tokencode": REQUIRED! 
  102.     // set identical to the incoming parameter 
  103.     $tokencode = $_SESSION['tokencode']; 
  104.  
  105.     // parameter "srvurl": REQUIRED! 
  106.     // set identical to the incoming parameter   
  107.     $srvurl = $_SESSION['srvurl']; 
  108.  
  109.     // parameter "endtime" : OPTIONAL 
  110.     // Format: yyyy-mm-dd hh:MM:ss  e.g. 2012-05-31 21:39:00 
  111.     // set this parameter to set the time to close the user's Internet connection  
  112.     // Note: the value must be url-encoded.   
  113. //  $endtime = urlencode('2012-05-31 21:39:00'); 
  114.     $endtime = '';   
  115.  
  116.     // parameter "postauth" : OPTIONAL 
  117.     // E.g. http://www.YourDomain.com 
  118.     // set this parameter to redirect to a specified URL after authenticated. 
  119.     // Note: the value should be url-encoded.   
  120.     //$postauth = urlencode("http://www.wiwiz.com"); 
  121.     $postauth = '';  
  122.  
  123.     $parameters = "?wiwiz_auth_api=1&ver=1.0"// parameter "wiwiz_auth_api" and "ver". Fixed value 
  124.             "&tokencode="$tokencode . // parameter "tokencode". See above 
  125.             "&userkey="$userkey .     // parameter "userkey". Set your own User Key 
  126.             "&action="$action .       // parameter "action". See above 
  127.             "&endtime="$endtime .     // parameter "endtime". See above 
  128.             "&postauth="$postauth;    // parameter "postauth". See above 
  129.  
  130.     $verifycode = file_get_contents($srvurl . $parameters); 
  131.  
  132.     if (strpos ($verifycode"ERR") === 0) { 
  133.         // if there is an error, show error code 
  134.         echo "Error: "$verifycode
  135.  
  136.     } else { 
  137.         // OK, now. do Step 3. 
  138.  
  139.         //**************************************************** 
  140.         // Step 3. Complete the Authentication by calling Wiwiz Auth API 
  141.         //****************************************************   
  142.         $redirectUrl = $srvurl.     // use the value of incoming parameter "srvurl" as the redirection address 
  143.                 "?wiwiz_auth_api_login=1".  // parameter "wiwiz_auth_api_login" 
  144.                 "&tokencode="$tokencode . // parameter "tokencode", set identical to the incoming parameter    
  145.                 "&verifycode="$verifycode;    // parameter "verifycode", set identical to the incoming parameter   
  146.         ob_start(); 
  147.         header("Location: "$redirectUrl); // finally, do the redirection 
  148.         ob_flush(); 
  149.  
  150. //          echo "<script>location.href=\"". $redirectUrl ."\"</script>"; 
  151.  
  152.     } 
  153.  
  154.  
  155. ?> 

 

本文出自 “野獸技術博客” 博客,請務必保留此出處http://beastwu.blog.51cto.com/5091229/865707

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章