用Discuz/UCenter賬號實現Wifi登錄認證

如果你有一個用Discuz/UCenter構建的網站,還有自己的Wifi基站或熱點,那你可以用Wiwiz爲你的Wifi熱點做一個入口登錄頁面。當 wifi終端用戶連到你的熱點時,打開任何網頁都會先顯示你的wifi登錄頁面,也就是web認證頁面,然後輸入他在你的網站的用戶名和密碼之後才能通過 認證。並且認證之後,瀏覽器會跳轉到網站首頁。
效果如下圖:

 

Web認證頁面

 

認證後跳轉至網站首頁

實現方法如下:
用PHP寫web登錄頁,調用Wiwiz Auth API(需要Wiwiz專業版)。
我的登錄頁的文件名是myauth.php,將其放置在服務器的discuz根目錄上即可。
myauth.php代碼如下:

  1. <?php 
  2. /* 
  3. * 用Discuz(UCenter)用戶賬號實現Wifi Portal認證(Web認證) 
  4. * 調用Wiwiz Auth API 
  5. * 作者:[email protected] 
  6. */ 
  7.  
  8. $userkey = "XXXXXXXXXXXXXXXXXX";    //替換爲你的Wiwiz User Key 
  9.   
  10. //**************************************************** 
  11. // Gets incoming parameters 
  12. //**************************************************** 
  13.   
  14. $pTokencode = $_REQUEST["tokencode"];   // incoming parameter "tokencode" 
  15. $pSrvurl = $_REQUEST["srvurl"];     // incoming parameter "srvurl" 
  16.   
  17. session_start(); 
  18. if($pTokencode != null)  
  19.     $_SESSION['tokencode'] = $pTokencode
  20. if($pSrvurl != null) 
  21.     $_SESSION['srvurl'] = $pSrvurl
  22. ?> 
  23.   
  24. <html> 
  25. <head> 
  26. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  27. <meta http-equiv="Content-Language" content="zh"
  28. <meta http-equiv="Pragma" content="no-cache"
  29. <meta http-equiv="Cache-Control" content="no-cache"
  30. <title> Discuz無線網絡認證 </title> 
  31. </head> 
  32. <body> 
  33.   
  34. <form method="post"
  35. <center> 
  36. <b><h2>Discuz無線網絡</h2><br><br> 
  37.  
  38. 請使用網站賬號進行認證<br></b> 
  39.       
  40. 用戶名: <input type="text" name="username" /> 
  41. <br> 
  42.   
  43. 密碼: <input type="password" name="password" /> 
  44. <br> 
  45.   
  46. <input type="submit" name="login" value="登錄/認證" /> 
  47. <br> 
  48. </center> 
  49.  
  50. <?php 
  51. if(isset($_REQUEST['login'])) { // if "Login" button is clicked 
  52.   
  53.     //**************************************************** 
  54.     // Step 1. Do your business. E.g. check user login ...  
  55.     //**************************************************** 
  56.     define('UC_CONNECT''mysql');  // 連接 UCenter 的方式: mysql/NULL, 默認爲空時爲 fscoketopen() 
  57.                                     // mysql 是直接連接的數據庫, 爲了效率, 建議採用 mysql 
  58.  
  59.     //數據庫相關 (mysql 連接時, 並且沒有設置 UC_DBLINK 時, 需要配置以下變量) 
  60.     define('UC_DBHOST''localhost');               // UCenter 數據庫主機 
  61.     define('UC_DBUSER''root');                    // UCenter 數據庫用戶名 
  62.     define('UC_DBPW''');                          // UCenter 數據庫密碼 
  63.     define('UC_DBNAME''discuz');                  // UCenter 數據庫名稱 
  64.     define('UC_DBCHARSET''UTF-8');                // UCenter 數據庫字符集 
  65.     define('UC_DBTABLEPRE''discuz.pre_ucenter_'); // UCenter 數據庫表前綴 
  66.  
  67.     //通信相關 
  68.     define('UC_KEY''');               // 與 UCenter 的通信密鑰, 要與 UCenter 保持一致 
  69.     define('UC_API''');               // UCenter 的 URL 地址, 在調用頭像時依賴此常量 
  70.     define('UC_CHARSET''gbk');        // UCenter 的字符集 
  71.     define('UC_IP''');                // UCenter 的 IP, 當 UC_CONNECT 爲非 mysql 方式時, 並且當前應用服務器解析域名有問題時, 請設置此值 
  72.     define('UC_APPID', 1);              // 當前應用的 ID 
  73.      
  74.     require_once './uc_client/client.php'
  75.  
  76.     if(isset($_POST['login'])) { 
  77.         list($uid$username$password$email) = uc_user_login($_POST['username'], $_POST['password']); 
  78.         if($uid > 0) { 
  79.             $loginSuccess = true; 
  80.         } else { 
  81.             $loginSuccess = false; 
  82.         } 
  83.     }  
  84.   
  85.     if($loginSuccess == false) { 
  86.   
  87.         echo "用戶名或密碼錯誤!";   // if user login failed, show an error message 
  88.   
  89.     } else { 
  90.   
  91.         //**************************************************** 
  92.         // Step 2. Do the pre-auth by calling Wiwiz Auth API 
  93.         // IMPORTANT: Do this on your server side(ASP, C#, JSP/Servlet, PHP...),  
  94.         //            but DO NOT do this on your client side (HTML/Javascript) 
  95.         //**************************************************** 
  96.   
  97.         // parameter "action" : REQUIRED! 
  98.         // set it to "1" to authenticate the user 
  99.         // set it to "0" to block the user 
  100.         $action = "1"
  101.   
  102.         // parameter "tokencode": REQUIRED! 
  103.         // set identical to the incoming parameter 
  104.         $tokencode = $_SESSION['tokencode']; 
  105.   
  106.         // parameter "srvurl": REQUIRED! 
  107.         // set identical to the incoming parameter   
  108.         $srvurl = $_SESSION['srvurl']; 
  109.   
  110.         // parameter "endtime" : OPTIONAL 
  111.         // Format: yyyy-mm-dd hh:MM:ss  e.g. 2012-05-31 21:39:00 
  112.         // set this parameter to set the time to close the user's Internet connection  
  113.         // Note: the value must be url-encoded.   
  114.         $endtime = "";  //urlencode('2012-05-31 21:39:00');     //設置wifi使用期限 
  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://192.168.1.250/home");     //wifi認證後跳轉至網站首頁 
  121.   
  122.         $parameters = "?wiwiz_auth_api=1&ver=1.0"// parameter "wiwiz_auth_api" and "ver". Fixed value 
  123.                 "&tokencode="$tokencode . // parameter "tokencode". See above 
  124.                 "&userkey="$userkey .     // parameter "userkey". Set your own User Key 
  125.                 "&action="$action .       // parameter "action". See above 
  126.                 "&endtime="$endtime .     // parameter "endtime". See above 
  127.                 "&postauth="$postauth;    // parameter "postauth". See above 
  128.   
  129.         $verifycode = file_get_contents($srvurl . $parameters); 
  130.   
  131.         if (strpos ($verifycode"ERR") === 0) { 
  132.             // if there is an error, show error code 
  133.             echo "Error: "$verifycode
  134.   
  135.         } else { 
  136.             // OK, now. do Step 3. 
  137.   
  138.             //**************************************************** 
  139.             // Step 3. Complete the Authentication by calling Wiwiz Auth API 
  140.             //****************************************************   
  141.             $redirectUrl = $srvurl.     // use the value of incoming parameter "srvurl" as the redirection address 
  142.                     "?wiwiz_auth_api_login=1".  // parameter "wiwiz_auth_api_login" 
  143.                     "&tokencode="$tokencode . // parameter "tokencode", set identical to the incoming parameter    
  144.                     "&verifycode="$verifycode;    // parameter "verifycode", set identical to the incoming parameter   
  145.             ob_start(); 
  146.             header("Location: "$redirectUrl); // finally, do the redirection 
  147.             ob_flush(); 
  148.   
  149. //          echo "<script>location.href=\"". $redirectUrl ."\"</script>"; 
  150.   
  151.         } 
  152.   
  153.     } 
  154. ?> 
  155.   
  156. </form> 
  157.   
  158. </body> 
  159. </html> 

 

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