我正在尝试将 mediawiki 集成到我的网站中,但遇到了麻烦。我认为问题与 cookie 有关,因为我从 mediawiki API 获得了成功。
这是我的代码:
function mw_session_manager($Action = "")
{
$Root = $_SERVER['SERVER_ADDR'];
$API_Location = "${Root}/w/api.php";
$expire = 60*60*24*14 + time();
$CookieFilePath = tempnam("/tmp/thedirectory", "CURLCOOKIE");
$CookiePrefix = 'theprefix';
$Domain = 'thedomain';
if($Action == 'login')
{
// Retrieves email address and password from sign-in form
$Email = $_POST['LogInEmail'];
$LgPassword = $_POST['LogInPassword'];
// Query to retrieve username from database based on email. It is implied that authentication has already succeeded.
$Query = "SELECT Username FROM Accounts WHERE Email = '$Email'";
$ResultSet = mysql_query($Query);
$ResultArray = mysql_fetch_array($ResultSet);
$LgName = $ResultArray[0]; // Username
// set variables to use in curl_setopts
$PostFields = "action=login&lgname=$LgName&lgpassword=$LgPassword&format=php";
// first http post to sign in to MediaWiki
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_POSTFIELDS, "$PostFields");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ResultSerialized = curl_exec($ch);
curl_close($ch); // curl closed
$ResultUnserialized = unserialize($ResultSerialized);
$Token = $ResultUnserialized[login][token];
// cookie must be set using session id from first response
$WikiSessionID = $ResultUnserialized[login][sessionid];
setcookie("${CookiePrefix}_session", $WikiSessionID, $expire, '/', $Domain);
// second http post to finish sign in
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_POSTFIELDS, "action=login&lgname=${LgName}&lgpassword=${LgPassword}&lgtoken=${Token}&format=php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ResultSerialized = curl_exec($ch);
curl_close($ch); // curl closed
$ResultUnserialized = unserialize($ResultSerialized);
// set persistent cookies
$LgToken = $ResultUnserialized["login"]["lgtoken"];
$LgUserID = $ResultUnserialized["login"]["lguserid"];
$LgUserName = $ResultUnserialized["login"]["lgusername"];
setcookie("${CookiePrefix}UserName", $LgUserName, $expire, '/', $Domain);
setcookie("${CookiePrefix}UserID", $LgUserID, $expire, '/', $Domain);
setcookie("${CookiePrefix}Token", $LgToken, $expire, '/', $Domain);
// Delete cURL cookie
unlink($CookieFilePath);
return $somedebuggingvariable;
}
if($Action = "logout")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_POSTFIELDS, "action=logout");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ResultSerialized = curl_exec($ch);
$LogoutReturn = unserialize($ResultSerialized);
$_SESSION['APIReturn'] = $LogoutReturn;
curl_close($ch); // curl closed
// destroys persistent cookies and ends session
$expire = time() - 60*60*24*90;
setcookie('Session', '', $expire, '/', $Domain);
setcookie("${CookiePrefix}_session", '', $expire, '/', $Domain);
setcookie("${CookiePrefix}UserName", '', $expire, '/', $Domain);
setcookie("${CookiePrefix}UserID", '', $expire, '/', $Domain);
setcookie("${CookiePrefix}Token", '', $expire, '/', $Domain);
// delete cURL cookie
unlink($CookieFilePath);
}
}
我还注意到,如果我提供了错误的令牌,API 仍然会返回成功,所以我也不能排除这种情况。
编辑:我现在已经让它完美地工作并将代码更新为当前的工作代码。