I cannot comment on Jon's post due to lack of privileges, so I am posting a new answer to explain in detail about its use of the MediaWiki API and the transmission of cookies. Hope this helps someone.
You can use the PHP cURL library to transfer the session cookie value as a cookie to the api.php page on your wiki (you need to create the full URL for cURL to get the page). The session cookie name is the value of $ wgSessionCookie (which is set and not used by default) or $wgCookiePrefix . '_session' $wgCookiePrefix . '_session' ($ wgCookiePrefix is ββset to false by default and the database name is used by default). Therefore, based on the setting, use the appropriate value.
I use api.php?action=query&format=xml&meta=userinfo , and then look for the user ID that is returned by the wiki (Note format = xml ). The identifier 0 means that the user is anonymous.
Here is the complete code for the function used (I understand that I am not checking some possible error conditions). You probably have to change the value of $ session_cookie
function isLoggedIn() { $session_cookie = 'wikidb_session'; if(!isset($_COOKIE[$session_cookie])) { return false; } $url = ((isset($_SERVER['HTTPS']))?'https://':'http://') . $_SERVER['HTTP_HOST'] . (($_SERVER['SERVER_PORT'] != 80)?':' . $_SERVER['SERVER_PORT']:'') . '/wiki/api.php?action=query&format=xml&meta=userinfo'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_COOKIE, $session_cookie . '=' . $_COOKIE[$session_cookie]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $ret = curl_exec($ch); curl_close($ch); return preg_match('/id="(\d+)"/',$ret,$id) && $id[1]; }
Note. If you only check if anon = "or id =" 0 "is returned by calling api.php, in case the call returns something unexpected or api.php is not in the URL, the function will notify the user of the login so itβs better to check the returned id.
source share