大网数据广告活动
PHP 语言与运行手册

Pdo\Sqlite::setAuthorizer · Configures a callback to be used as an authorizer to limit what a statement can do

· 52CE 我爱测

Pdo\Sqlite::setAuthorizer — Configures a callback to be used as an authorizer to limit what a statement can do

Pdo\Sqlite::setAuthorizer

(PHP 8 >= 8.5.0)

Pdo\Sqlite::setAuthorizer — Configures a callback to be used as an authorizer to limit what a statement can do

说明

public function Pdo\Sqlite::setAuthorizer(?callable $callback): void

Sets a callback that will be called by SQLite every time an action is performed (reading, deleting, updating, etc.). This is used when preparing an SQL statement from an untrusted source to ensure that the statement does not access data it is not allowed to see or execute malicious statements that damage the database.

The authorizer is used only during the statement preparation phase. It may be called several times for a single statement: a SELECT or UPDATE query calls it for every column that would be read or updated. It runs again whenever SQLite re-prepares a statement, which may happen while the statement is being executed, for instance after the schema has changed.

The authorizer is called with up to five arguments. The arguments received are described at SQLite3::setAuthorizer().

Only a single authorizer can be in place on a database connection at a time. Each call to this method overrides the previous one. The authorizer is disabled by default, and can be disabled again by setting a null callback.

The callback must not modify the database connection that invoked it.

More details can be found in the » SQLite documentation.

注意:

This method is the equivalent of SQLite3::setAuthorizer(), except that it returns void instead of bool.

参数

callback

The callable to be invoked, or null to disable the current authorizer callback.

It must return one of Pdo\Sqlite::OK, Pdo\Sqlite::DENY, or Pdo\Sqlite::IGNORE. When Pdo\Sqlite::DENY is returned, the statement that triggered the authorizer fails with an error stating that access is denied. When Pdo\Sqlite::IGNORE is returned for a read action, the statement is prepared so that a null value is substituted for the column that would have been read.

返回值

没有返回值。

错误/异常

This method itself does not throw, but if the authorizer callback does not return an int, or returns an int which is not one of Pdo\Sqlite::OK, Pdo\Sqlite::DENY, or Pdo\Sqlite::IGNORE, the statement preparation throws a TypeError or a ValueError respectively.

示例

示例 #1 Pdo\Sqlite::setAuthorizer() example

Only read actions are allowed on the connection. The action codes are exposed as SQLite3 class constants, which requires the SQLite3 extension to be available; their integer values may be used directly otherwise.

<?php
$db = new Pdo\Sqlite('sqlite::memory:');
$db->exec('CREATE TABLE users (id, name)');

$db->setAuthorizer(function (int $action, ...$args) {
    return match ($action) {
        SQLite3::SELECT, SQLite3::READ => Pdo\Sqlite::OK,
        default => Pdo\Sqlite::DENY,
    };
});

var_dump($db->query('SELECT name FROM users') instanceof PDOStatement);

try {
    $db->exec('DROP TABLE users');
} catch (PDOException $e) {
    echo $e->getMessage(), "\n";
}
?>

以上示例的输出类似于:

bool(true)
SQLSTATE[HY000]: General error: 23 not authorized

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。