Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
1.2k views
in Q2A Core by
For example, I want to write a chat module and I would like it to play off the status of if a user is logged in and what their level is (admin, mod, registered)  

So what is the easiest way to to just return their username and level or if they are not logged in let it report that? I can work out everything else if I can just get those out of a blank page.

1 Answer

+1 vote
by
I'm assuming here that your code will be running as part of a Q2A page request, and not as a separate page or iframe. If so, you have two global variables that you can access - $qa_login_userid and $qa_login_level - to get the info you need. To check if a user is logged in, use:

if (isset($qa_login_userid)) { ... }

If a user is logged in, to check if they're an editor, use:

if ($qa_login_level>=QA_USER_LEVEL_EDITOR) { ... }

QA_USER_LEVEL_EDITOR can be substituted with any other constant from the top of qa-app-users.php.

One caveat - If you're writing your code inside a function block, you'll need to add this just after the function opens to access those variables:

global $qa_login_userid, $qa_login_level;
by
Actually. I was hoping to do it from a separate page.  Like a page sitting next to index.php in the root directory.
by
edited by
Hmmm... this is a bit more involved. Assuming it's on the same level as index.php, your file would have to start with something like this:

<?
    define('QA_BASE_DIR', dirname(empty($_SERVER['SCRIPT_FILENAME']) ? __FILE__ : $_SERVER['SCRIPT_FILENAME']).'/');

    require 'qa-include/qa-base.php';
    require_once 'qa-include/qa-app-users.php';

    function qa_db_fail_handler()
    {
        echo "A database error occurred.";
        exit;
    }

    qa_base_db_connect('qa_db_fail_handler');
    $qa_login_user=qa_get_logged_in_user($qa_db);
    $qa_login_userid=@$qa_login_user['userid'];
    $qa_login_level=@$qa_login_user['level'];
    qa_base_db_disconnect();

    // now you're free to do what you like, including using the if(...)s I mentioned above
?>

[usual caveat: I haven't actually tested this]
by
I figured it out.

I just cloned index.php and called it test.php and did the same with qa-index.php. From there I just changed what is in the body section and it is doing what I want now. It is pretty much a clean slate that knows if a user in logged in and who they are. Pretty much what I was looking for.
by
I gave that a shot but it failed. That might be because I applied the changes here.

http://www.question2answer.org/qa/599/installation-error

URLs are wonky on my site but it mostly works.

Here is the error I get when I try what is mentioned above:
Warning: require(QA_INCLUDE_DIRqa-base.php) [function.require]: failed to open stream: No such file or directory in /home/content/n/e/r/nerocorvo/html/STFU/test1.php  on line 4

Fatal error: require() [function.require]: Failed opening required 'QA_INCLUDE_DIRqa-base.php' (include_path='.:/usr/local/php5/lib/php') in /home/content/n/e/r/nerocorvo/html/STFU/test1.php on line 4
by
Actually I had something wrong in the code above and have fixed it now, so feel free to try again.
by
Awesome.. That works perfectly. And I hell of a lot better then what I had before. Thanks.
...