Page load time with Cookie vs Session in PHP

I have a social network type site (member site). I store a lot of user data like username, user number, email, name, photo url, in the user session when the user logs in to reduce the number of DB requests per page. I sometimes hear people talk about using cookies for some things, I understand that cookies should never be used for username / passwords, but the main question is, does cookie slow down page load times? On the user's home page I have blocks that the user can drag and drop to rebuild the home page there, in my first version I saved the location of the blocks in a cookie, now I store this value in mysql.and I am building block layout in php on page load instead of using javascript to set starting locations. When I first used cookies to set locations there, even though the page was loading and the blocks would be there in their original location, the page would really quickly change the location of those blocks from the value obtained from the cookie, however this was very much slower and made the page appear very slow to load. I always thought the cookie was slow since then, but maybe it wasn't the cookie that slowed it down.the page would really quickly change the location of these blocks from the value it got from the cookie, however this was very much slower and made the page seem very slow to load. I always thought the cookie was slow since then, but maybe it wasn't the cookie that slowed it down.the page would really quickly change the location of these blocks from the value it got from the cookie, however this was very much slower and made the page seem very slow to load. I always thought the cookie was slow since then, but maybe it wasn't the cookie that slowed it down.

So cookies slow the page down?

+2


source to share


3 answers


If I understand correctly, you are saying that your page was slow when:

  • block positions were saved in cookies
  • and the blocks were placed in Javascript on page load

I think the "slow" part is not coming from cookies:

  • they are quite small compared to the page size
    • yes it adds some overhead but not that important for one page
    • becomes more important if you have a lot of resources (images, static files, ...) as they are sent with every request, although
  • they do not contain much data


But with this solution you might get the impression of being slow:



  • the page is loaded by the default block.
  • some JS code is executed to re-install the blocks
    • which means waiting for bits before re-positioning blocks
    • which takes some processing time for the JS code to execute
    • which also means that for the browser to re-draw the page multiple times (something changes every time).


IMHO, what makes your page "faster" now is that the client gets the entire page with already well-spaced blocks, which means there is no processing at all on page load :-)

Small / missing cookies can make little difference ... but not much.


If you want to know more about the size of cookies and their effect on loading time and some guidance, you can read this article from Yahoo: Performance Study Part 3: When Cookie Crumbles ; this is quite an interesting read and not too hard to understand.

+2


source


A cookie is just an extra HTTP header with key / value pairs. Parsing this header and populating the $ _COOKIE array does not add significant overhead (it almost certainly won't be a bottleneck).

If you store a large amount of data in a cookie (that is, not only the session ID), then it is worth remembering that this data is sent with every request to your domain, not only for PHP pages, but for images, CSS, JS, etc. ... For this reason, on a high-speed site, you can arrange for these "static" items from a different domain to reduce the impact of this overhead.



If this cookie is a session identifier, then PHP must retrieve that session from the file system, database, or other storage engine in order to populate the $ _SESSION array. It may take a little time, but it really depends on the mechanism used.

+3


source


Cookies will slightly increase the load time for your pages as the cookie data must be sent with every request.

For an HTML page, this won't make much of a difference, but if you have all of your assets on the same domain (as usual) it can make a significant difference.

In fact, reading a cookie in PHP shouldn't take any noticeable amount of time.

+2


source







All Articles