ColdFusion:何时定义会话变量?
当用户请求页面时,是否为该用户启动会话?据我所知,一旦发出页面请求,会话就会启动...
如果是这种情况,那么什么时候创建会话变量?即用户名、密码、偏好设置等...随时都可以吗?
When a user requests a page, is this when a session is started for that user? From what I can tell, a session is started as soon as you make a page request...
If this is the case, when do you create session variables? i.e. username, password, preferences, etc... just any time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,用户的会话范围是在第一个请求时设置的。然而,这取决于您何时想要设置各种标志和值的偏好。不过,您可能不想将密码放入会话范围内。
我喜欢做的是将用户特定值放入用户结构中。因此,在请求开始时,我会检查变量并设置(如果不存在)。例如...
当用户登录时,您可以填写适当的值并设置 session.user.authorized = true
当用户注销时,这种方法的好处是您可以删除用户结构。
然后在下一页,将再次检查用户结构,如果不存在则创建。
Yes the session scope for the user is setup on the first request. However it depends on your preference as to when you want to set various flags and values. You probably don't want to put password in the session scope though.
What I like to do is put user specific values in a user struct. So on request start I'd check for the variable and setup if it doesn't exist. For example...
When the user logs in you can then fill in the appropriate values and set session.user.authorized = true
When the user logs out the nice thing about this approach is you can just delete the users struct.
Then on the next page the check will be made again for the user struct and created if it doesn't exist.
配置和使用会话变量的基础知识此处进行了说明 。
会话基本上是客户端和应用程序之间的逻辑关系,因此会话变量可从客户端对应用程序的第一个请求获取,并且会话通常使用唯一标识会话的 cookie 跨请求持久保存,尽管也可以这样做通过在 url 中传递标识符。值得注意的是您的会话超时设置,例如您可能希望为不设置 cookie 的机器人/爬虫提供更短的超时,因此将为每个页面请求启动一个新会话 (更多详细信息)。
The basics of configuring and using session variables are explained here.
A session is basically a logical relationship between a client and an application, so session variables are available from the client's first request to your application, and the session is typically persisted across requests using cookies which uniquely identify the session, although this can be also done by passing the identifiers in the url. It's worth paying attention to your session timeout settings, for example you may want to provide a shorter timeout to bots/crawlers which don't set cookies and will therefore initiate a new session for each page request (more details on this).