cookies-判断用户是否是第一次进入页面

在郑州美莱的活动项目中客户当时有提到该需求。尽管最后去掉了该需求,但是还是花了我不少时间研究,因为之前的项目我前端没有用到过cookies,吓死宝宝了

$(document).ready(function() {
var newVisitor = isNewVisitor();
if(newVisitor === true) {
// 动画弹出消息框
// alert('您是新用户!');
// 标记:已经向该访客弹出过消息。30天之内不要再弹
setCookie("gznotes-visited", "true", 5);
} else {
console.log("您已不是新用户了")
}
});

function isNewVisitor() {
// 从cookie读取“已经向访客提示过消息”的标志位
var flg = getCookie("gznotes-visited");
if(flg === "") {
return true;
} else {
return false;
}
}

// 写字段到cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
}

// 读cookie
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while(c.charAt(0) == ' ') c = c.substring(1);
if(c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
注:这是我在网上找到的,但是我已经忘了是在哪里看到的了

转载于:https://www.cnblogs.com/liuqingxia/p/8081778.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断用户是否第一次访问,可以为每个用户设置一个唯一的标识符,例如使用浏览器中的cookie或者在服务器端使用session。使用FastAPI,可以通过以下方式获取和设置cookie和session: 获取cookie: ```python from fastapi import FastAPI, Request app = FastAPI() @app.get("/") async def read_root(request: Request): cookie_value = request.cookies.get("user_id") if cookie_value: return {"message": "Welcome back!"} else: return {"message": "Welcome for the first time!"} ``` 设置cookie: ```python from fastapi import FastAPI, Response app = FastAPI() @app.get("/") async def read_root(response: Response): response.set_cookie(key="user_id", value="unique-user-id") return {"message": "Cookie set!"} ``` 设置session: ```python from fastapi import FastAPI, Request, Response, Depends from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.responses import RedirectResponse from fastapi.templating import Jinja2Templates from typing import Optional app = FastAPI() security = HTTPBasic() templates = Jinja2Templates(directory="templates") fake_users_db = { "johndoe": { "username": "johndoe", "full_name": "John Doe", "email": "johndoe@example.com", "hashed_password": "fakehashedsecret", "disabled": False, }, "alice": { "username": "alice", "full_name": "Alice Wonderson", "email": "alice@example.com", "hashed_password": "fakehashedsecret2", "disabled": True, }, } def get_current_user(session_token: Optional[str] = None): if not session_token: return None username = session_token.split(":")[0] user = fake_users_db.get(username) if user: return user return None @app.get("/") async def read_root(request: Request, current_user: Optional[dict] = Depends(get_current_user)): if current_user: return {"message": "Welcome back, " + current_user["full_name"] + "!"} else: return {"message": "Welcome for the first time!"} @app.post("/login") async def login(response: Response, credentials: HTTPBasicCredentials = Depends(security)): correct_username = credentials.username in fake_users_db correct_password = False if correct_username: user = fake_users_db[credentials.username] correct_password = credentials.password == user["hashed_password"] if not (correct_username and correct_password): return {"error": "Incorrect username or password", "response": response} session_token = credentials.username + ":" + credentials.password response.set_cookie(key="session_token", value=session_token) return RedirectResponse(url="/") @app.post("/logout") async def logout(response: Response): response.delete_cookie(key="session_token") return RedirectResponse(url="/") ``` 在这个示例中,如果用户登录成功,会设置一个名为session_token的cookie,其中保存了用户名和密码的组合。然后在每次请求时,都会检查该cookie是否存在,并且使用该cookie中的信息来识别当前用户

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值