Hudzilla.org - the homepage of Paul Hudson
Contents > Output Buffering Wish List | Report Bug | About Me ]

13.11     URL rewriting: output_add_rewrite_var() and output_reset_rewrite_vars()

This is NOT the latest copy of this book; click here for the latest version.

bool output_add_rewrite_var ( string name, string value)

bool output_reset_rewrite_vars ( void )

Once or twice so far we've looked at functions where I've said something like, "you might not realise it now, but this is a really useful function and it will grow on you." These two functions are, uniquely, quite the reverse: they appear incredibly useful on the surface, but it's actually quite hard to find working examples of them in action. That's not to say they are useless: far from it! You will know when you need these functions. Of course, you can't know you need them if you don't know they exist, which is why they are here!

What they do is cause your URLs, forms, and frames to be rewritten so that they will pass in variables and values of your choosing. It does this by using output buffering and parsing any HTML A elements (links) plus any FORM elements and FRAMES elements and appending fields to URLs contained therein. This is best demonstrated with some code, so here goes:

<?php
  output_add_rewrite_var
('foo', 'baz');
  echo
'<A HREF="foo.php">Click here!</A><BR />';
  
output_add_rewrite_var('bar', 'baz');
  echo
'<A HREF="foo.php">Click here!</A><BR />';

  echo
'<FORM ACTION="foo.php" METHOD="POST">';
  echo
'<INPUT TYPE="BUTTON" VALUE="  Click here!  " />';
  echo
'</FORM>';
?>

<A HREF="foo.php">Click here!</A>

Now, when you run that you should find that the URLs have been rewritten to point to "http://localhost/foo.php?foo=baz&bar=baz". What's more, both links are the same: the fact that you printed out one link before adding the second variable is irrelevant, thanks to output buffering.

The form will actually have extra hidden fields in there for your values, effectively giving the same result. The best part is that PHP always leaves the forms and URLs working as they did before: any fields in your forms or variables in your URLs will remain there untouched.

Now, what the output_reset_rewrite_vars() function does is undo the effects of your calls to output_add_rewrite_var(). One call to output_reset_rewrite_vars() wipes out any variables you've added to URLs and FORMs - it goes back and changes them all to be without the added variables.

Here's the same script again, except this time with output_reset_rewrite_vars() tacked on the end:

<?php
  
echo '<A HREF="foo.php">Click here!</A><BR />';
  
output_add_rewrite_var('foo', 'baz');
  echo
'<A HREF="foo.php">Click here!</A><BR />';
  
output_add_rewrite_var('bar', 'baz');
  echo
'<A HREF="foo.php">Click here!</A><BR />';

  echo
'<FORM ACTION="foo.php" METHOD="POST">';
  echo
'<INPUT TYPE="BUTTON" VALUE="  Click here!  " />';
  echo
'</FORM>';
?>

<A HREF="foo.php">Click here!</A>

<?php
  output_reset_rewrite_vars
();
?>

That will print out all URLs and the form as written, without the "foo" and "bar" variables.

Now, as you can see, using these two seems like a great way to transfer data around your site. Question is, why would you want to? I won't tell you these functions aren't ever used, because I'm sure there are legitimate reasons that are so obscure I can't think of them! Still, it's best you at least know these guys exist.





<< 13.10 Compressing output   13.12 Summary >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
fredrik at demomusic dot nu - 30 Aug 2008

There is at least one perfect legitimate reason to use this: I have a webpage and is going to let parts of it be included via iframes on other pages. For the script to recognize that it is run in an iframe, I append a "?site=foo" to all links if it once has been run whit that querystring-value. Now I can add append this to all links on a conditional basis without having to hard-code an appender on all links throughout the site.

A PHP User - 30 Aug 2008

One (possibly) usefull purpose concerns sessionids when cookies cannot be used. Sessionids are typically stored in cookies on the users computer. But some browsers cannot accept cookies, or users might have disabled them. In that case, output_add_rewrite_var is sometimes used to append the sessionid to urls.

Of course this causes *huge* security issues.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow