Siva Bathula
Siva Bathula

Reputation: 766

Client only cookies - cookie which doesn't ever go to the server

I want to store user-clicked data in a cookie which never has to go to the server. Its like a session-added data, which I want to persist over sessions, as in the data just keeps adding to the cookie, and it is stored for a long time, and only gets deleted when the user removes browsing history. The cookie is pure-client only cookie and it never has to go the server, as I don't need the user generated data at the server, so I want to get rid of the additional overhead the cookie creates in sending back and forth between browser and server. Is it possible to achieve this?

Upvotes: 35

Views: 13232

Answers (4)

Franz Deschler
Franz Deschler

Reputation: 2574

I know it´s a little late for you, but this answer is for all who have the same problem.

  1. With HTML5 you can use web storage.

  2. (Just an idea! - not tested!) You could define a cookie (via javascript on client) and set the "secure"-attribute. In this case, the cookie will only be sent to the server on HTTPS connections. To make sure the cookie never leaves the browser, you just never open a HTTPS connection ;-)

EDIT

Now it´s 2022 and I would not recommend solution 2) anymore. Instead of setting the secure flag on the cookie, set the path to a path on the server which is never used.

Upvotes: 16

Dan Smith
Dan Smith

Reputation: 5685

If browser compatibility is a concern you can use a some javascript to wrap around various different technologies. Older versions of IE support (supprise supprise) a proprietary version of localstorage called userData (I don't think it's exactly the same, but should do what you need).

A wrapper script like https://github.com/andris9/jStorage or https://github.com/marcuswestin/store.js should do what you need it to do.

Upvotes: 9

Ma Jerez
Ma Jerez

Reputation: 6025

Use an impossible path to set the cookie:

document.cookie = "cookieName=...; expires=... ; path=/never_reached/ablkappmqlnahsuia";

Upvotes: 2

jzkpk
jzkpk

Reputation: 41

I'm 100% sure that there is no way to force cookies to be client-side only, they are allways sent to server. There is however possible to do the opposite: server-side only cookies (not readable by javascript) by setting HttpOnly flag on cookie.

Upvotes: 4

Related Questions