简体   繁体   English

javascript中的用户会话跟踪?

[英]user session tracking in javascript?

I have an app in phonegap (pure js, html, css) where user signs in and then it has access to some aditional sites. 我在phonegap(纯js,html,css)中有一个应用程序,用户可以在其中登录,然后可以访问一些附加站点。 My question is how to save his personal data or his session. 我的问题是如何保存他的个人数据或会话。 There is no php involved for sending cookie or handling sessions. 没有涉及发送cookie或处理会话的php。 Everything need to be saved in JS (jQuery). 一切都需要保存在JS(jQuery)中。 How can I do this? 我怎样才能做到这一点?

You can use HTML5 feature Local Storage 您可以使用HTML5功能本地存储

For eg; 例如

When user logs in, set one variable as isAuthenticated in your local storage 用户登录时,在本地存储中将一个变量设置为isAuthenticated

By default, it will be 0, on successful login, set it to 1. 默认情况下为0,成功登录后将其设置为1。

localStorage.isAuthenticated = 0; // default
OR
localStorage.setItem('isAuthenticated',0);

On Successful login 成功登录后

localStorage.isAuthenticated = 1; // default
OR
localStorage.setItem('isAuthenticated',1);

And whenever you want to check user's authenticity, simply compare value of isAuthenticated variable from Local Storage. 而且,每当您要检查用户的真实性时,只需比较Local Storage中isAuthenticated变量的值即可。

var isLoggedIn = localStorage.getItem('isAuthenticated'); var isLoggedIn = localStorage.getItem('isAuthenticated');

if(isLoggedIn){
    //your code
} else {
    // invalid user code
}

UPDATE If you are storing javascript objects then 更新如果您要存储javascript对象,则

var myObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('myObject', JSON.stringify(myObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('myObject');
retrievedObject = JSON.parse(retrievedObject)

Convert your object into string using JSON.stringify and then store it into the local storage. 使用JSON.stringify将对象转换为string ,然后将其存储到本地存储中。 While retrieving cast that string back into the javascript object using JSON.parse 当检索cast那个string返回到javascript object使用JSON.parse

Looking at the local storage documentation, the functionality seems to be limited to handle only string key/value pairs. 查看本地存储文档,该功能似乎仅限于处理字符串键/值对。

You can take a look at the documentation here 您可以在这里查看文档

Apple 苹果

Mozilla Mozilla

PS : Phonegap has nothing to do with working of local storage or any other HTML feature. PS:Phonegap与本地存储或任何其他HTML功能无关。 Phonegap is just a middleware that facilitates communication between your device hardware and your javascript/html code. Phonegap只是一种中间件,可促进设备硬件与javascript / html代码之间的通信。

Here's the PhoneGap documentation on LocalStorage and SQL Database: 这是有关LocalStorage和SQL数据库的PhoneGap文档:

http://docs.phonegap.com/en/2.0.0/cordova_storage_storage.md.html#localStorage http://docs.phonegap.com/en/2.0.0/cordova_storage_storage.md.html#localStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM