如何在php中禁用会话? [英] How to disable session in php?

查看:34
本文介绍了如何在php中禁用会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

session_status 有一种可能的状态,称为 PHP_SESSION_DISABLED .

there is one possible status for session_status called PHP_SESSION_DISABLED .

有什么特定的功能可以禁用php中的会话吗??

is there any specific function that can disable sessions in php??

推荐答案

备注:我多次更新此答案,添加了更多信息并删除了我发现错误的先前句子.在最后一次编辑中,我完全重新编写了它,删除了错误的句子和对 PHP 源代码的引用.

Sessions 扩展的文档读取:

默认情况下,PHP 中启用会话支持.如果您不想使用会话支持构建 PHP,您应该指定 --disable-session 选项进行配置.

Session support is enabled in PHP by default. If you would not like to build your PHP with session support, you should specify the --disable-session option to configure.

调用 session_status() 或任何其他 会话函数 在使用 --disable-session 编译的 PHP 上触发 PHP 致命错误,由于函数不存在而停止脚本:

Calling session_status() or any other session function on a PHP compiled with --disable-session triggers a PHP Fatal Error that stops the script because the function does not exist:

$ php -m | grep session
$ php -r 'session_start();'
PHP Fatal error:  Call to undefined function session_start() in Command line code on line 1

文档还说:

Windows 版本的 PHP 已内置对此扩展的支持.您无需加载任何其他扩展即可使用这些功能.

The Windows version of PHP has built-in support for this extension. You do not need to load any additional extensions in order to use these functions.

这可能意味着无法从 Windows 上的 PHP 中删除会话功能.

This probably means there is no way to remove the sessions functionality from PHP on Windows.

您可以通过为 session.save_handlersession.serialize_handlerphp.ini 中.

为了测试,您可以设置 session.save_handler,例如,在命令行中使用 -d 选项;它覆盖从 php.ini 读取的值:

For testing you can set session.save_handler, for example, in the command line using the -d option; it overrides the value read from php.ini:

$ php -d session.save_handler=foo -r 'session_start(); var_dump(session_status() == PHP_SESSION_DISABLED);'
PHP Warning:  session_start(): Cannot find save handler 'foo' - session startup failed in Command line code on line 1
PHP Stack trace:
PHP   1. {main}() Command line code:0
PHP   2. session_start() Command line code:1
bool(true)

如您所见,session_start() 触发警告,抱怨处理程序无效且会话状态为 disabled(无法启动).

As you can see session_start() triggers a warning complaining about the handler not being valid and the session status is disabled (it cannot start).

如果您尝试在运行时为 session.save_handler 设置无效值,ini_set() 会触发警告并且不会更改该值.

If you try to set an invalid value to session.save_handler at runtime, ini_set() triggers a warning and doesn't change the value.

$ php -r 'ini_set("session.save_handler", "foo"); session_start(); var_dump(session_status() == PHP_SESSION_ACTIVE);'
PHP Warning:  ini_set(): Cannot find save handler 'foo' in Command line code on line 1
PHP Stack trace:
PHP   1. {main}() Command line code:0
PHP   2. ini_set() Command line code:1
bool(true)

会话处于活动.成功启动.

但是,即使处理程序在 php.ini 或命令行中设置为无效,PHP 代码也可以在调用 session_start() 之前修复它:

However, even if the handler is set as invalid in php.ini or in the command line, the PHP code can fix it before it calls session_start():

$ php -d session.save_handler=foo -r 'ini_set("session.save_handler", "files"); session_start(); var_dump(session_status() == PHP_SESSION_ACTIVE);'
bool(true)

同样,session_start() 成功,会话处于 active.

Again, session_start() succeeded, the session is active.

您可以通过为 session.save_handlersession.serialize_handlerphp.ini 中.

请注意,如果这些值中的任何一个无效,session_start() 将触发 PHP 警告.

Please note that if any of these values is invalid, session_start() triggers a PHP Warning.

然而,因为这两个设置都可以从任何地方修改(PHP_INI_ALL 的意思是php.ini, httpd.conf, .htaccess, PHP code),它们可以是,如好吧,从 PHP 代码中设置回有效值,以这种方式取消任何禁用会话的努力.

However, because both these settings can be modified from everywhere (PHP_INI_ALL means php.ini, httpd.conf, .htaccess, PHP code), they can be, as well, set back to valid values from the PHP code, cancelling this way any effort to disable sessions.

显然没有办法强制禁用会话,除了在没有会话支持的情况下编译 PHP,如上所述.

这篇关于如何在php中禁用会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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