用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

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