会话变量与普通变量? [英] session variable vs normal variable?

查看:70
本文介绍了会话变量与普通变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别?我知道会话变量存储信息并让您跨页面使用它,但我有几个问题:值存储在哪里?会话变量和普通变量有什么区别?

What's the difference? I know that session variables store information and let you use it across pages, but I've got a few questions: Where is the value stored? And what is the difference between a session variable and a normal variable?

这是在 PHP 的上下文中.我正在寻求深入的答案,因为我没有在网上找到很多关于此的信息.

This is in the context of PHP. I'm after an in-depth answer because I've not found alot of information about this online.

推荐答案

值存储在哪里?

这取决于 PHP 配置.默认情况下,会话变量被序列化并写入服务器文件系统上的文件中.在启动会话的每个页面视图中,它们都被反序列化并可从 $_SESSION 数组访问.可以覆盖默认会话处理程序,以便您可以将变量存储在其他地方,例如数据库.

That depends on the PHP configuration. By default, session variables are serialized and written into a file on the server's file system. On each page view that starts the session, they are unserialized and accessible from the $_SESSION array. It's possible to override the default session handler so that you can store the variables elsewhere, such as a database.

会话的工作原理是将会话 ID(这是一个唯一标识符)作为 cookie 存储在客户端的计算机上.每次客户端请求一个页面时,会话 ID cookie 会与请求一起发送,PHP 从 cookie 中获取会话 ID,然后拉取与该会话 ID 相关的会话数据.

Sessions work by storing a session ID (which is a unique identifier) as a cookie on the client's computer. Each time the client requests a page, the session ID cookie is sent along with the request, PHP picks up the session ID from the cookie, and then pulls the sesssion data that relates to said session ID.

会话变量和普通变量有什么区别?

what is the difference between a session variable and a normal variable?

简单地说,会话变量被保存到源(例如文件系统),这就是它们在页面请求之间保持的方式.一个普通的变量只会在脚本执行完成后才存在,然后就会被销毁.

Simply put, a session variable gets saved to a source (such as file system), this is how they can persist between page requests. A normal variable will only live until the execution of the script completes, it will then be destroyed.

这篇关于会话变量与普通变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆