Are cookies the only native repository of subdomains?

Cookies are great because the value written on the website can be used at www.website.com (www is considered sudomain of no-www). The downside is that all cookie values โ€‹โ€‹are sent along with every HTTP request to the server. Therefore, I am looking for a local storage mechanism, available initially for Javascript, which works cross-subdomain and is not transmitted to the server. Is there such a mechanism? LocalStorage does not work cross-subdomain, and Flash Cookies will not work on the iPhone.

+7
source share
2 answers

Perhaps just redirecting the website to www.website.com or vice versa? This seems to be the easiest solution. http://www.scriptalicious.com/blog/2009/04/redirecting-www-to-non-www-using-htaccess/

+3
source

If your users have a real account on which they log into the system on your server, you can save information on the server side and simply include a little javascript on each page that will need data with the corresponding data. When you create a page on the server side, you can define the user object in javascript with the corresponding attributes set to data values, which can then be specified on the client side. Thus, you include only the data that is needed on this page, the same user data is available, no matter which computer the user logs on to (do not rely on persistent cookies). If you need a large amount of data only occasionally, and you do not want to include it on the page if necessary, then make these pieces of data requested through ajax / json so that they can be received only when it was necessary.

If you still intend to store it only locally, then your cookies or HTML5 local storage are your only options, and cookies will be your only cross-browser option that will cover all browsers used. When adding complexity to the implementation, you can combine a number of suggestions:

  • Always redirect to www.domain.com, so all user actions are in the same domain.
  • Use local HTML5 storage, if available (redirecting in step 1 prevents blocking of subdomains).
  • Return to the cookie store when local HTML5 storage is not available.

One could write or find an abstraction for local HTML5 storage and cookies, so 99% of your code can be independent of which storage engine was actually used. There seem to be some jQuery plugins that do just that.

+1
source

All Articles