1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Seting a cookie on login and remove it on logout

Discussion in 'XenForo Development Discussions' started by Nuno, Jul 1, 2015.

  1. Nuno

    Nuno Active Member

    Hello,

    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 :) ?

    Thanks
     
  2. Chris D

    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:
    Code:
    </xen:hook>
    Add:
    Code:
    <xen:if is="{$visitor.user_id}">
        var expires = new Date(new Date().getTime() + 7 * 86400000); // 7 days
        $.setCookie('nameofcookie', 'valueofcookie', expires);
    </xen:if>
    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.
     
    Nuno likes this.
  3. Nuno

    Nuno 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.

    Thanks
     

Share This Page