Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
2.5k views
in Plugins by

After successfully install LDAP plugin from Zakkak, the view counts of my questions become all to be 1 (one). The IP of the viewers are all to be the LDAP server IP.

How can I get view couting work normally with LDAP login?

Q2A version: 1.7

1 Answer

+2 votes
by
edited by

I don't have the appropriate infrastructure to test this but I find this very related to issues in which load balancers are involved. Client executes a requests, goes through the LB and then an application node receives the request from the LB using that IP address.

This situations can be solved by configuring the LB (I think all of them do that by default) to set the X-Forwarded-XYZ HTTP headers. Then you process them in the application. Many frameworks do that by default.

I guess you could follow a similar approach where instead of using the LB you'd be using the LDAP (I don't know if you can modify or configure it to set those HTTP headers) and then, in your Q2A application, you'd have to modify the qa_remote_ip_address() function in qa-base.php (either by hacking the core or creating an override plugin) so that it takes the IP address from the previous HTTP header.

Plan B, would be to remove the IP check and increase those views with every refresh by removing these 2 lines in qa-include/pages/question.php:

( ($question['lastviewip']!=qa_remote_ip_address()) || (!isset($question['lastviewip'])) ) &&
( ($question['createip']!=qa_remote_ip_address()) || (!isset($question['createip'])) ) &&

by
I tried to remove the line but it doesn't work for me.
by
The funtion `qa_remote_ip_address()` content is very simple, it just returns @$_SERVER['REMOTE_ADDR'] - searching for $_SERVER['REMOTE_ADDR'] results in ONE place in qa_base.php i.e. the same location only. I'm stuck there :-(
by
Oh, of course. You have to remove the 'createip' line too because it will always be the same too! I've updated the answer to reflect that in Plan B.
Regarding qa_remote_ip_address() I don't understand what you mean. You need to set the X-Forwarded-For HTTP header in your LDAP and then parse the header in that function.
by
I tried commenting off both of your recommended lines and still not get it counted though :(
by
That works for sure... you must be using the same user who posted the question and that never counts, regardless of the LDAP, and it is OK. Easy way to test it: open a private browsing session and refresh a couple of times :)
by
@pupi1985 I have retried in Incognito tab and the view count remains 1 Huhu
by
I'll try the `X-Forwarded-XYZ HTTP headers` then
by
Triple check that. I have a clean install. I removed those 2 lines. Posted a question with a given user. Opened a private browsing session. Refreshed the page a couple of times and it got incremented (everything from the same IP, which was my localhost)
by
Great check. Thank you! Please also install LDAP plugin.
by
I can't. I mean, I've no LDAP server configured. Anyway, I hardly believe the plugin is changing something related to the view count you could remove the whole IF statement near those lines and only leave the line with the:
$qa_content['inc_views_postid']=$questionid;
That will always increase view counts without considering any condition. If that actually works, then you can see exactly which condition is failing there. EG: Maybe the user agent you're using is not in the list for the qa_is_human_probably() to consider it as a human.
...