CakeFest 2024: The Official CakePHP Conference

output_add_rewrite_var

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

output_add_rewrite_var添加 URL 重写器的值

说明

output_add_rewrite_var(string $name, string $value): bool

如果“URL-Rewriter”输出缓冲处理程序未激活,此函数将启动它,存储 namevalue 参数,并在缓冲区刷新时根据有效的 ini 设置重写 URL 和表单。此函数后续的调用将存储所有附加的名/值对,直到处理程序关闭。

当冲刷输出缓冲区(调用 ob_flush()ob_end_flush()ob_get_flush() 或在脚本结束时)时,'URL-Rewriter' 处理程序会将名/值对作为查询参数添加到 HTML 标签属性的 URL 中,并根据 url_rewriter.tagsurl_rewriter.hosts 配置指令的值向表单添加隐藏字段。

添加到 'URL-Rewriter' 处理程序的每个名/值对都会添加到 URL 或表单,即使这会导致重复的 URL 查询参数或具有相同名称属性的元素。

注意: 一旦关闭了 'URL-Rewriter' 处理程序,就无法再次启动。

参数

name

变量名。

value

变量值。

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
7.1.0 自 PHP 7.1.0 起,使用专用的输出缓冲区, url_rewriter.tags 仅用于输出函数,并且 url_rewriter.hosts 可用。在 PHP 7.1.0 之前,由 output_add_rewrite_var() 设置的重写变量共享具有透明 session id 支持的输出缓冲区(请参阅 session.trans_sid_tags)。

示例

示例 #1 output_add_rewrite_var() 示例

<?php
ini_set
('url_rewriter.tags', 'a=href,form=');

output_add_rewrite_var('var', 'value');

// 一些链接
echo '<a href="file.php">link</a>
<a href="http://example.com">link2</a>'
;

// 表单
echo '<form action="script.php" method="post">
<input type="text" name="var2" />
</form>'
;

print_r(ob_list_handlers());
?>

以上示例会输出:

<a href="file.php?var=value">link</a>
<a href="http://example.com">link2</a>

<form action="script.php" method="post">
<input type="hidden" name="var" value="value" />
<input type="text" name="var2" />
</form>

Array
(
    [0] => URL-Rewriter
)

参见

add a note

User Contributed Notes 3 notes

up
2
Anonymous
15 years ago
For a completely valid XHTML document you have to set the arg_separator, use this before you use output-add-rewrite-var:

<?php
ini_set
('arg_separator.input', '&');
ini_set('arg_separator.output', '&');
?>
up
2
Niko
16 years ago
This function also adds a parameter to <input type="image"> fields!

Example:
This code:

<?
output_add_rewrite_var ('var','value');
echo '<form action="" method="post">
<input type="image" src="image.jpg" alt="go">
</form>';
?>

will output something like this:

<form action="" method="post">
<input type="hidden" name="var" value="value">
<input type="image" src="image.jpg?var=value" alt="go">
</form>
up
-7
Bruce
16 years ago
Just to clarify...

session.use_trans_sid does NOT need to be enabled in order for output_add_rewrite_var() to work.
To Top