如果你有一個用Discuz/UCenter構建的網站,還有自己的Wifi基站或熱點,那你可以用Wiwiz爲你的Wifi熱點做一個入口登錄頁面。當 wifi終端用戶連到你的熱點時,打開任何網頁都會先顯示你的wifi登錄頁面,也就是web認證頁面,然後輸入他在你的網站的用戶名和密碼之後才能通過 認證。並且認證之後,瀏覽器會跳轉到網站首頁。
效果如下圖:
Web認證頁面
認證後跳轉至網站首頁
實現方法如下:
用PHP寫web登錄頁,調用Wiwiz Auth API(需要Wiwiz專業版)。
我的登錄頁的文件名是myauth.php,將其放置在服務器的discuz根目錄上即可。
myauth.php代碼如下:
- <?php
- /*
- * 用Discuz(UCenter)用戶賬號實現Wifi Portal認證(Web認證)
- * 調用Wiwiz Auth API
- * 作者:[email protected]
- */
- $userkey = "XXXXXXXXXXXXXXXXXX"; //替換爲你的Wiwiz User Key
- //****************************************************
- // 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;
- ?>
- <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> Discuz無線網絡認證 </title>
- </head>
- <body>
- <form method="post">
- <center>
- <b><h2>Discuz無線網絡</h2><br><br>
- 請使用網站賬號進行認證<br></b>
- 用戶名: <input type="text" name="username" />
- <br>
- 密碼: <input type="password" name="password" />
- <br>
- <input type="submit" name="login" value="登錄/認證" />
- <br>
- </center>
- <?php
- if(isset($_REQUEST['login'])) { // if "Login" button is clicked
- //****************************************************
- // Step 1. Do your business. E.g. check user login ...
- //****************************************************
- define('UC_CONNECT', 'mysql'); // 連接 UCenter 的方式: mysql/NULL, 默認爲空時爲 fscoketopen()
- // mysql 是直接連接的數據庫, 爲了效率, 建議採用 mysql
- //數據庫相關 (mysql 連接時, 並且沒有設置 UC_DBLINK 時, 需要配置以下變量)
- define('UC_DBHOST', 'localhost'); // UCenter 數據庫主機
- define('UC_DBUSER', 'root'); // UCenter 數據庫用戶名
- define('UC_DBPW', ''); // UCenter 數據庫密碼
- define('UC_DBNAME', 'discuz'); // UCenter 數據庫名稱
- define('UC_DBCHARSET', 'UTF-8'); // UCenter 數據庫字符集
- define('UC_DBTABLEPRE', 'discuz.pre_ucenter_'); // UCenter 數據庫表前綴
- //通信相關
- define('UC_KEY', ''); // 與 UCenter 的通信密鑰, 要與 UCenter 保持一致
- define('UC_API', ''); // UCenter 的 URL 地址, 在調用頭像時依賴此常量
- define('UC_CHARSET', 'gbk'); // UCenter 的字符集
- define('UC_IP', ''); // UCenter 的 IP, 當 UC_CONNECT 爲非 mysql 方式時, 並且當前應用服務器解析域名有問題時, 請設置此值
- define('UC_APPID', 1); // 當前應用的 ID
- require_once './uc_client/client.php';
- if(isset($_POST['login'])) {
- list($uid, $username, $password, $email) = uc_user_login($_POST['username'], $_POST['password']);
- if($uid > 0) {
- $loginSuccess = true;
- } else {
- $loginSuccess = false;
- }
- }
- 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'); //設置wifi使用期限
- // 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://192.168.1.250/home"); //wifi認證後跳轉至網站首頁
- $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>";
- }
- }
- }
- ?>
- </form>
- </body>
- </html>