Seting a cookie on login and remove it on logout


Active member

I don't to much experience in php developing, but I would like to try to create an addon so I can save a cookie when a member login and remove it when the member logouts.

My goal is to know if it's a guest or a member, since at the moment it's not possible.

I already read some post and resources here and I think this can be done using a Model, but I'm not sure.

Can some one give some highlights were to start :) ?


Chris D

XenForo developer
Staff member
You quite possibly do not need to use PHP.

If you set a cookie with XenForo's JS, it can probably be done directly in the template and they are automatically cleared when a user logs out.

Just find the template: page_container_js_body

And below:
<xen:if is="{$visitor.user_id}">
    var expires = new Date(new Date().getTime() + 7 * 86400000); // 7 days
    $.setCookie('nameofcookie', 'valueofcookie', expires);
Just change 'nameofcookie' to what you want the name of the cookie to be. 'valueofcookie' to what you want the value to be. And change the "7" in the expires var to the number of days you want the cookie to be valid for.


Active member
Hi Chris,

I'm already doing that way, but since I want to use this cookie to decide if the page can be cached or not, I'm not sure if I should cache pages without a cookie, since this can be a spider or a member with js disabled.