您的位置:首页 > 其它

用Discuz/UCenter账号实现Wifi登录认证

2012-08-05 00:22 351 查看
如果你有一个用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
* 作者:tiida_2011@163.com
*/

$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>


本文出自 “野兽技术博客” 博客,请务必保留此出处http://beastwu.blog.51cto.com/5091229/954612
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: