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

I want to create and set custom cookie whenever user visits the question page.

So, How can I create and set custom cookie on question page load q2a.

Actually I am creating a plugin to count number of visitss by user and store it in cookie and update every time. But when I refresh the question page it is not incrementing by +1 rather it is incrrementing +3, so first time when I visit the page counter cookie vale set to 0 but when second time I visit the page the cookie value was found 3.

I think on page refesh cookie value is updating three times but I want only single increment everytime.

Here is my code
I have added this code in my qa-plugin.php file.
<?php

$qa_count_user_visits = 0;    

if(!isset($_COOKIE['qa_count_user_visits'])){

    setcookie('qa_count_user_visits', $qa_count_user_visits, time() + 86400 * 365, '/', QA_COOKIE_DOMAIN, (bool)ini_get('session.cookie_secure'), true);

}

else{

    $qa_count_user_visits= (int) $_COOKIE['qa_count_user_visits'] + 1;

    setcookie('qa_count_user_visits', $qa_count_user_visits, time() + 86400 * 365, '/', QA_COOKIE_DOMAIN, (bool)ini_get('session.cookie_secure'), true);

}

please help me

Q2A version: 1.8.6
by
There are a few issues here.
 1. Cookies can be modified client side. So anyone could change the value programatically and the server will trust on whatever value it receives
 2. The "visit" concept is blurry. If I refresh a page on your server twice, should it count as one visit or as two visits?
 3. You don't seem to be saving the information anywhere. You need to create a table with a foreign key to `userid` in the `^users` table and the counter. Then in each page refresh, increment the counter (most likely a layer would be the most appropriate here)

More specifically about the 3 increments, if you load absolutely anything that would bootstrap Q2A, then the code will execute and increment the value on 3. I'd rather research why you're hitting your server 3 times with a single page refresh rather than focusing on the plugin itself
by
Hi, Thank you for your reply!

1. I know cookies can be modified client-side programmatically but in my case using cookies for storing visit counts does not harm if cookies are modified client-side because I am not using it for any critical task.

2. If you refresh the page twice on my server-side it will count as two visits.

3. I think calling insert and update query every time on page refresh to count visit will be expensive in case of the database. So in my opinion cookies will be the best fit.

Did you find the reason why on a single page refresh the cookie is set three times?
by
If you please help me where can I create cookie in Q2A so it can be called only once on every question page refresh.
by
qa-plugin.php will be called once per request that bootstraps Q2A

1 Answer

+1 vote
by
selected by
 
Best answer

If you refresh a page you are firing many requests. Most likely, some of those requests are executing the qa-blob.php or qa-image.php files (for example, because of the use of avatars). So you are bootstrapping Q2A several times on a "page refresh".

Whether this makes sense to you or not, depends on your requirements. If you want to only execute that code once maybe it might be better for you to add it to a layer. For example:

public function initialize() {
    parent::initialize();
    $qa_count_user_visits = isset($_COOKIE['qa_count_user_visits']) ? (int)$_COOKIE['qa_count_user_visits'] + 1 : 1;
    setcookie('qa_count_user_visits', $qa_count_user_visits, time() + 86400 * 365, '/', QA_COOKIE_DOMAIN, (bool)ini_get('session.cookie_secure'), true);
}
by
Hi pupi1985, Is there any way I can initialize the above function as given in your answer only on a specific q2a page.
by
Yes, but if you have more questions, let's avoid having an epic comment thread here and ask a different question, clarifying exactly what you want (you can link questions to answers by clicking on the appropriate button on the answer)
by
Sounds good! Thank you:)
...