How to set cookie with .htaccess?

by 5 replies
19
Hello,

I need to set a cookie across an entire site. Meaning any page visited on the site sets the cookie. I want it to last as long as possible.

I'm told this can be done with .htaccess.

Do you know the code that would set a site-wide cookie using .htaccess?

Also...

Is it possible to set a redirect in the .htaccess file based on a cookie?

Something like... "If cookie exits redirect to URL"

Do you have the code for that?

Thanks
#programming #cookie #htaccess #set
  • Thanks, but I'm still way confused about how to set the cookie expiration. And make the cookie accessible by third party websites...

    If this was the code...
    Header set Set-Cookie "example-cookie=true"

    What would I need to add to that code to make it expire Today's date + 365 days, and accessible by third party websites?

    Thanks
  • As far as I know, you can't specifically set a cookie to be accessed by third party websites. The third party website needs to set their own cookies. You can let them to do that if you include something from the third party website in the content of your website, such as a banner.

    Source: HTTP cookie - Wikipedia, the free encyclopedia

    The syntax is:

    Set-Cookie <name>=<value>[; <name>=<value>]...[; expires=<date>][;domain=<domain_name>][; path=<some_path>][; secure]

    Example:

    Header set Set-Cookie "example-cookie=true;expires=Sat, 01-Jan-2011 00:00:00 GMT;domain=mydomain.com;path=/;"

    Not sure how to do the +365 days, perhaps there is some .htaccess guru here who knows?

    If you find out, please post the answer here
  • You can use verbose time periods for expiry date instead of the explicit date: 1 month, 3 days, 2 hours, 1 year, etc.
    • [ 1 ] Thanks
    • [1] reply
    • Hi Steve,

      Do you have an example? I can't seem to get it to work.

      I've tried...
      expires=+365 day
      expires=+365 days
      expires=365 days

      None of those set an expiration date.

Next Topics on Trending Feed