Sessions in JS or jQuery

Asked

Viewed 11,265 times

5

Is there any way to use js or jquery sessions? I’d like to do something similar to php sessions.

  • 3

    As far as I know is not possible. Javascript is client-side. Session is accessed server-side. I may be wrong and there is a way, so I leave it to you to comment. Only if you mount some server-side script with session values and send it to the client. But directly I believe it is not possible.

3 answers

7


There is no safe way to do this on the client side.

You can create a Cookie but it is a limited and insecure tool. Cookie allows a string with chaves->valores but can be modified by user or other applications.

You can also use the Localstorage API which is a relatively new API:

localStorage.setItem('lastname','Smith'); // gravar
Storage.removeItem('lastname');           // apagar a entrada "lastname"
Storage.clear();                          // apagar tudo o que está no local storage
alert(localStorage.getItem('lastname'));

and thus save data. But it is also insecure since the user or other application can change that content.

You can read more about the API here (link).

  • and to finish this localStorage, that is, clean the data?

  • @Furlan added more info

  • Thank you @Sergio

  • Could also use sessionStorage, for actual session :P

4

There are several ways to keep information on JS:

  • Cookies: the oldest and most common form. Easy to set a variable, but requires a little extra code to get or remove. It has space limit and is sent together in requests to the server. It is through cookies that PHP keeps the user session id.

  • Webstorage: introduced with HTML5. It has a much higher storage limit than cookies, and a simpler user interface. It is subdivided into two types: sessionStorage (keeps the data only for the window where it was created and is lost when it is closed) and localStorage (keeps the data for all windows and has no expiration date, only being removed by code or directly by the user).

  • Indexeddb: used primarily for larger data storage. There is no storage limit, but some browsers ask for the user’s permission after reaching a certain amount (usually 50Mb). It works asynchronously, a benefit for application performance and a complicator for coding, although with js we’ve gotten used to asynchronous. To make coding easier, you can use a framework, such as pouchDB.

  • Websql: works as an SQL database. It is the specification prior to Indexeddb and is no longer maintained by W3C.

For your case, I believe the best change is to use localStorage, but it’s up to you to assess the application needs.

2

You can use Cookies if your application is not storing much information beyond the fact that you should pay particular attention to the security and the way in which you will store this information, since using Cookies the data is on the client’s side and for this check and validate the data on each request is never too much.

Alternatively you can also use localStorage which also stores the data on the client side and offers more advantages, such as larger storage space, no expiration date and an extremely simple-to-use API.

Observing: PHP’s SESSION’S are stored in a folder ON THE SERVER defined in php.ini, but for PHP to identify the SESSION it uses a session-id which is stored in a cookie and is sometimes found in the url’s of some applications (usually found as a parameter called PHPSESSID).

You can check this behavior using Google Chrome and look in the Resources tab on any site that uses Sesssions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.