Serving images from cookie less domain

CS4 Production Premium

To improve web page performance it is good idea to serve static content from a cookie less domain.

What is Cookies less domain?

Domain which don’t require any cookies from client to serve the request is called cookie less domain. For example if we need to serve static images, there is no need to get or set any cookie, not even a session cookie.

Why not cookies?

Cookies normally are small amount of text stored on client. Cookies can be set for some domain or specific path in domain. If any cookies are set, browser will send these with each and every request made to display page, this includes all images, CSS, JS files etc.

For example if a page have 20 images and 1 CSS the browser will send 22 request to server. If cookies are of size 2kb, in total it will cause 44kb of network traffic for each page served. This will not only consume visitor bandwidth but also cause unnecessary delays for the visitor.

If we can serve images from a separate domain or possibly a sub domain with no cookies, we can save significant bandwidth and visitor’s time.

How to create cookie less domain?

To ensure this, when setting cookies make sure that you set cookies for particular domain. For example set cookies for www.mydomain.com, and server images from images.mydomain.com. If you set cookies for a subdomain or separate domain, they will not be available on other domains. So cookies set for www.mydomain.com will not be sent with all images called from images.mydomain.com.

PHP function setcookie allows to set cookies for particular domain. For example following code setcookie("TestCookie", $value, time()+3600, "/", "www.mydomain.com"); will set cookie which is only available for www.mydomain.com and not for images.mydomain.com. In contrast the code setcookie("TestCookie", $value, time()+3600, "/", ".mydomain.com"); will set cookies for all sub domains of mydomain.com.

Who is already using cookie less domains?

  1. Yahoo!: Servers images from yimg.com, a cookie less domain
  2. YouTube: Servers images from ytimg.com, a cookie less domain
  3. Amazon: Servers images from images-amazon.com, a cookie less domain

Yahoo.com! using yimg.com for images with no cookies. See request headers which don’t have any cookies being sent.

12 Responses to “Serving images from cookie less domain”

  1. The funny thing using Google Page Speed is that Google told me that I should use Cookieless sub domain but all the cookies sent are Google Analytics, Google Adv… cookies

    So the best web site optimization is just remove all Google products from the page!

  2. If Google Analytics (GA) is used in page it will create few cookies. I think there is some option available in GA to limit the cookies to specific domain, you should try that. If you limit GA cookies to your main domain, they will not be sent with all images called.

  3. Please where and how do i need to add the code in the post above? Is it function.php?

  4. Block Country IP on June 21st, 2010 at 12:26 pm

    thank you, subscribed!

  5. No, the code is just a sample. you need to find similar code/function in your site and make it use a specific domain, so other sub domains can become cookie less domains

  6. how if we use like co.cc as free domain as cookieless domain, can we?

    thanks

  7. Yes any domain can be used, as image domain does not matter much.

  8. Hi !

    nice article and well explained .

    I ran into lots of articles to serve static content from a cookieless domain , but this one seems to be the easiest for me to follow.

    I am using vbulletin software , although I do not know where to find this PHP function , but it should be there some where.

    now I am facing two issues here :

    the first one is that I have to move files physically to their proper subdomain folders right ?

    the second one is , I am wondering if I moved all images , css , java etc to a cookie-less sub domain , how do I overcome the issue of ( having posts around with the old images urls like ( http://www.mydomian.com/images/whatever.jpg ).

    cause I don’t allow my community to upload images on any other server.

    Thank you again for sharing such useful article .

  9. If you are using linux machine you can overcome this issue by using soft links. In this way you images will be available from both (main and subdomain) so you new images will be moved to subdomain and old files will still accessible. Another method will be to rewrite old urls to new domain through .htaccess.

  10. [...] Serving images from cookie less domain [...]

  11. Hello,

    I am trying the same for my website. So i created a static.mywebsite.com subdomain. I still see the message about cookieless domain in the “page speed” addon for mozzila.
    Weird since i dont set any cookies on my website? I do use a session.
    Commenting the session_start() in the beginning of my php script removes the “cookieless domain” warning. Un-commenting session_start() in my php script results in giving me the freakin warning again.
    Seems like a session is setting cookies too??? I dont get it.

    Can anyone explain this to me? And what should i do to fix it?

    Greetings,

    Marc

  12. @marc
    The message is there because session cookies is being set for *.mywebsite.com and not limited to http://www.mywebsite.com. If you don’t need sessions than keep session_start() commented. At the moment i don’t know how to limit session cookie to a domain (as this cookie is not set from set_cookie() function), I think there must be some configuration to tell domain of the session cookie. Please try to set cookie “PHPSESSID” via set_cookie function and give domain param as http://www.mywebsite.com before the session_start, I am not sure if it will work but no harm in trying.

Leave a Reply