How to set cookie with .htaccess?

5 replies
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
#cookie #htaccess #set
  • {{ DiscussionBoard.errors[2072314].message }}
  • Profile picture of the author Mirnova
    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
    {{ DiscussionBoard.errors[2074972].message }}
  • Profile picture of the author Manfred Ekblad
    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.

    When viewing a Web page, images or other objects contained within this page may reside on servers besides just the URL shown in your browser. While rendering the page, the browser downloads all these objects. Most modern websites that you view contain information from lots of different sources. For example, if you type www.domain.com into your browser, widgets and advertisements within this page are often served from a different domain source. While this information is being retrieved, some of these sources may set cookies in your browser. First-party cookies are cookies that are set by the same domain that is in your browser's address bar. Third-party cookies are cookies being set by one of these widgets or other inserts coming from a different domain.
    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
    {{ DiscussionBoard.errors[2075690].message }}
  • Profile picture of the author SteveJohnson
    You can use verbose time periods for expiry date instead of the explicit date: 1 month, 3 days, 2 hours, 1 year, etc.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[2075779].message }}
    • Profile picture of the author Mirnova
      Originally Posted by SteveJohnson View Post

      You can use verbose time periods for expiry date instead of the explicit date: 1 month, 3 days, 2 hours, 1 year, etc.
      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.
      {{ DiscussionBoard.errors[2076701].message }}

Trending Topics