Session 处理
简介
会话支持在 PHP 中是在并发访问时由一个方法来保存某些数据。
一个访问者访问你的 web 网站将被分配一个唯一的 id,就是所谓的会话 id。 这个 id 可以存储在用户端的一个 cookie 中,也可以通过 URL 进行传递。
会话支持允许你将请求中的数据保存在超全局数组$_SESSION中。 当一个访问者访问你的网站,PHP 将自动检查(如果 session.auto_start 被设置为 1)或者在你要求下检查(明确通过 session_start()) 当前会话 id 是否是先前发送的请求创建。如果是这种情况, 那么先前保存的环境将被重建。
如果你打开了 session.auto_start 那么 将对象放入会话的唯一方法是使用 auto_prepend_file 来加载定义这个对象的类,其中,在加载的定义的类时,你不得不使用 serialize() 你得对象,并且事后 unserialize() 它。
$_SESSION (和所有已注册得变量) 将被 PHP 使用内置的序列化方法在请求完成时 进行序列化。序列化方法可以通过 session.serialize_handler 这个 PHP 配置选项中来设置一个指定的方法。注册的变量未定义将被标记为未定义。在并发访问时,这些变量不会被会话模块 定义除非用户后来定义了它们。
因为会话数据是被序列化的,resource 变量不能被存储在会话中。
序列化句柄 (php
和 php_binary) 会受到 register_globals 的限制。
而且,数字索引或者字符串索引包含的特殊字符(|
和 !) 不能被使用。使用这些字符将脚本执行关闭时的最后出现错误。
php_serialize 没有这样的限制。
注意:
请注意当会话工作时,会话的记录并没有被创建 直到一个变量已经被添加一个新元素到 $_SESSION 全局数组中。 这点一直有效,无论是否使用 session_start() 函数 来开始会话。
- 安装/配置
- 预定义常量
- 示例
- Session 上传进度
- 会话和安全
- Session 函数
- session_abort — 丢弃会话数组的更改并结束会话
- session_cache_expire — 返回/设置当前缓存的到期时间
- session_cache_limiter — 读取/设置缓存限制器
- session_commit — session_write_close 的别名
- session_create_id — 创建新的会话 ID
- session_decode — 解码会话数据
- session_destroy — 销毁一个会话中的全部数据
- session_encode — 将当前会话数据编码为字符串
- session_gc — 执行会话数据垃圾回收
- session_get_cookie_params — 获取会话 cookie 参数
- session_id — 获取/设置当前会话 ID
- session_module_name — 获取/设置会话模块名称
- session_name — 读取/设置会话名称
- session_regenerate_id — 使用新生成的会话 ID 更新现有会话 ID
- session_register_shutdown — 关闭会话
- session_reset — 使用会话存储中的原始值重新初始化会话数组
- session_save_path — 读取/设置当前会话的保存路径
- session_set_cookie_params — 设置会话 cookie 参数
- session_set_save_handler — 设置用户自定义会话存储函数
- session_start — 启动新会话或者重用现有会话
- session_status — 返回当前会话状态
- session_unset — 释放所有的会话变量
- session_write_close — Write session data and end session
- SessionHandler — The SessionHandler class
- SessionHandler::close — Close the session
- SessionHandler::create_sid — Return a new session ID
- SessionHandler::destroy — Destroy a session
- SessionHandler::gc — Cleanup old sessions
- SessionHandler::open — Initialize session
- SessionHandler::read — Read session data
- SessionHandler::write — Write session data
- SessionHandlerInterface — The SessionHandlerInterface class
- SessionHandlerInterface::close — Close the session
- SessionHandlerInterface::destroy — Destroy a session
- SessionHandlerInterface::gc — Cleanup old sessions
- SessionHandlerInterface::open — Initialize session
- SessionHandlerInterface::read — Read session data
- SessionHandlerInterface::write — Write session data
- SessionIdInterface — The SessionIdInterface interface
- SessionIdInterface::create_sid — Create session ID
- SessionUpdateTimestampHandlerInterface — The SessionUpdateTimestampHandlerInterface interface
- SessionUpdateTimestampHandlerInterface::updateTimestamp — Update timestamp
- SessionUpdateTimestampHandlerInterface::validateId — Validate ID
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
