Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+5 votes
6.1k views
in Q2A Core by

I do not want unregistered users to have permission to view anything but the login / register screen.  This functionality does not seem to be built in. Therefore should I use something like:

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

5 Answers

+3 votes
by

Hi there. This is an interesting use case. Look in qa-page-question.php, in the area marked with the comment "Get information about this question".

You want to add something like the following just before that:

if (!isset($qa_login_userid)) {

$qa_content=qa_content_prepare();
$qa_content['error']=qa_insert_login_links('Please ^1log in^2 or ^3register^4 to view this question', $qa_request);
return $qa_content;
}
 
Of course, you can change the message that appears.
by
I do not follow. :)
by
if (!isset($qa_login_userid)) {

$qa_content=qa_content_prepare();
$qa_content['error']=qa_insert_login_links('Please ^1log in^2 or ^3register^4 to view this question', $qa_request);
return $qa_content;
}

I added above code, however the content did not show even I logined
by
It is different for more recent versions of Q2A. Now this should work:

if (!qa_is_logged_in()) {
  $qa_content=qa_content_prepare();
  $qa_content['error']=qa_insert_login_links('Please ^1log in^2 or ^3register^4 to view this question', qa_request());
  return $qa_content;
}
by
Thank you for your reply!
I mean that guest can view question but cannot view answer, only editor, expert, moderator and admin can view answer.
+2 votes
by

Create a "qa-theme.php" file into your theme directory (in "qa-theme" folder), and try something like this:

<?php
    
    global $qa_login_userid;
    
    //    if user is not logged in, display only login buttons

    if (!isset($qa_login_userid)){
    
        class qa_html_theme extends qa_html_theme_base
        {
            function body_content()
            {
                $this->body_prefix();

                $this->output('<DIV CLASS="qa-body-wrapper">', '');

                $this->header();
                #$this->sidepanel();

                #$this->main();
               
                #$this->footer();

                $this->output('</DIV> <!-- END body-wrapper -->');

                $this->body_suffix();
            }

            function header()
            {
                #$this->output('<DIV CLASS="qa-header">');

                #$this->logo();
                $this->nav_user_search();
                #$this->nav_main_sub();
                $this->header_clear();

                $this->output('</DIV> <!-- END qa-header -->', '');
            }

            function nav_user_search()
            {

                $this->nav('user');
                #$this->search();
            }

        }
    }

?>

by
And if you want to show the login and registration form, you may use something like (modify accordingly to your url setup, or filter those strings a bit):

                    if (qa_self_html() == './login?to=' or qa_self_html() == './register?to='){
                        $this->main();                   
                    }

(instead of that "#$this->main();" line).
by
If the content is only viewable by the logged in user will this impact the SEO of the site?
+1 vote
by

As of 1.4, this is supposedly a built-in option, but it didn't work for me - perhaps because of my Wordpress integration.  I solved this by creating my own theme and adding these lines which redirects back to the main site:


    class qa_html_theme extends qa_html_theme_base {
      
     function qa_html_theme($template, $content, $rooturl, $request) {
       qa_html_theme_base::qa_html_theme_base($template, $content, $rooturl, $request);
       
     }
      
       function body_content()
             {
               global $qa_login_userid;
               if (!isset($qa_login_userid)) {
             qa_redirect_raw("..");
           } else {
              parent::body_content();
           }
          
          
             }
    
    }
 

+4 votes
by
edited by

 

Open \qa-theme\NAME OF SELECTED TEMPLATE\qa-theme.php
 
insert the following code directly after the line which contains <?php
 
if (
  (!qa_is_logged_in()) and !(
    (strpos(qa_self_html(),'login') !== false )||
    (strpos(qa_self_html(),'forgot') !== false )||
    (strpos(qa_self_html(),'reset') !== false )
  )
) {
    qa_redirect('login');
}else{
 
and add the following codejust before the end of the file (but before ?> if it contains ?>)
}
by
This worked almost perfectly. However, I think it also prevents users from accessing the './forgot' page to reset their passwords. Any workaround for that?
by
I update the code above to allow access to the forgot page
+1 vote
by
edited by

My solution.

function doctype()
{
  if (!qa_is_logged_in()) {
    $req = qa_request();
    if (    $req != 'login' 
      && $req != 'register'
      && $req != 'forgot'
      && $req != 'reset'
      && $req != 'confirm'
      && $req != 'feedback'  // It is dependent on specification. 
      && $req != 'about'        // It is dependent on specification. 
      && $req != 'feed'          // It is dependent on specification. 
    ) {
      if ($req != '')
        qa_redirect_raw(qa_path_to_root().'login?to='.$req);  // for clicking URL of alartmail 
      else
        qa_redirect('login');
    }
  }
  qa_html_theme_base::doctype();
}
Also, Hiding side bar etc needs to be corresponded. 
...