PHP output_add_rewrite_var() Function

What is PHP output_add_rewrite_var() function?

If you want to add a new query variable with value to the URL as GET parameter or to the form as hidden input field, use output_add_rewrite_var() function.

Syntax:

output_add_rewrite_var(name, value)

Parameters:

This function has two optional parameters-

name (required) – Name of the variable.

value (required) – Value of the variable.

Return Values:

The function returns-

  • TRUE on success.
  • FALSE on failure.

Examples:

Example 1:

<?php
output_add_rewrite_var('user_id', 'USESS0921');
?>
<a href="confirm.php?act=TRUE">Click</a>
<form action="#" method="POST">
    <input type="text" name="Name" />
</form>

Explanation:

If you take the mouse over the “Click” link, you’ll see the new variable user_id with its value has been added to the link and it becomes –

http://localhost/phpexercise/confirm.php?act=TRUE&user_id=USESS0921

Also, in the source code you’ll see a new hidden field like this –

<input type=”hidden” name=”user_id” value=”USESS0921″>

Notes on output_add_rewrite_var() function:

  • If output buffering is turned off, the function will not work. PHP’s output buffering functionality allows to rewrite this URL.
  • If the function doesn’t work, check url_rewriter.tags and url_rewriter.hosts settings in php.ini file.
  • You can call this function maximum once per request.

PHP Version Support:

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

Summary: PHP output_add_rewrite_var() Function

output_add_rewrite_var() function is a built-in PHP output control function that allow you to add a new query variable with value to the URL as GET parameter or to the form as hidden input field.

Reference:

https://www.php.net/manual/en/function.output-add-rewrite-var.php