News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Single Sign On between SMF and Question2Answers.org

Started by ssaolcom, March 21, 2010, 04:58:47 PM

Previous topic - Next topic

ssaolcom

Hi,

I have an SMF forum and am now in the process of installing a question and answer (QA) tool. I want the QA tool to use the login information from SMF. In order to do this, I have to read the cookie that SMF has set, within a PHP file of QA. Using the template that QA provided, I had the following function within a QA PHP file:


function qa_get_logged_in_user($qa_db_connection)
{
session_start();

if ($_COOKIE["SMFCookie10"]) {
$result=mysql_fetch_assoc(
mysql_query(
"SELECT ID_MEMBER, memberName, emailAddress, ID_GROUP FROM smf_members WHERE ID_MEMBER=".
"(SELECT ID_MEMBER FROM smf_log_online WHERE session='".mysql_real_escape_string($_COOKIE["SMFCookie10"], $qa_db_connection)."')",
$qa_db_connection
)
);

if (is_array($result))
return array(
'userid' => $result['ID_MEMBER'],
'publicusername' => $result['memberName'],
'email' => $result['emailAddress'],
'level' => $result['ID_GROUP'] ? QA_USER_LEVEL_ADMIN : QA_USER_LEVEL_BASIC
);
}

return null;
}


When I run this, I get an error saying " Undefined index: SMFCookie10"
From the SMF admin panel, I saw that the cookie name was different, but when I used that instead of SMFCookie10 in the above PHP code, I still get the same error. So, I was hoping that $_COOKIE[SMFCookie10] would be the equivalent of $_COOKIE[$cookiename], since I cannot use the variable $cookiename within the QA PHP file, but this does not seem to work. Essentially, I am trying to retrieve the SMF cookie that stores the session ID of the SMF session. Would appreciate any help or pointers on what I am doing wrong. Thanks in advance.

ssaolcom

I learned that I need to use
if (@$_COOKIE[$cookiename])
instead of
if ($_COOKIE[$cookiename])

Also I used the information in a previous post here regarding reading data from a cookie. My function now looks like this, and it works.

function qa_get_logged_in_user($qa_db_connection)
{

session_start();

$cookiename = 'SMFCookie10';

if (isset($_COOKIE[$cookiename]) && preg_match('~^a:[34]:\{i:0;(i:\d{1,6}|s:[1-8]:"\d{1,8}");i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d{1,14};(i:3;i:\d;)?\}

$~', $_COOKIE[$cookiename]) === 1)
$var = @unserialize($_COOKIE[$cookiename]);
else
$var = @unserialize(stripslashes($_COOKIE[$cookiename]));

$member_id = $var[0];

if (@$_COOKIE[$cookiename]) {
$result=mysql_fetch_assoc(


mysql_query(
"SELECT ID_MEMBER, memberName, emailAddress, ID_GROUP FROM smf_members WHERE ID_MEMBER='$member_id'"
)


);

if (is_array($result))
return array(
'userid' => $result['ID_MEMBER'],
'publicusername' => $result['memberName'],
'email' => $result['emailAddress'],
'level' => $result['ID_GROUP'] ? QA_USER_LEVEL_ADMIN : QA_USER_LEVEL_BASIC
);
}

return null;

}



Thanks.

Advertisement: