PHP Magazine December Issue

02 Dec 2005

PHP Magazine just published their December issue. The cover article is an introduction to design patterns by Robert Peake. My column, Guru Speak, discusses the interesting things you can do with output buffering.

My favorite output buffering trick isn't really a trick at all - it's a relatively new (PHP 4.3+) function called output_add_rewrite_var(). This function makes the otherwise tedious chore of rewriting URLs very easy. For example, if you decide you want to propagate an auth token to strengthen your session mechanism, it's very easy:

  1.  
  2. output_add_rewrite_var('auth', '412e11');
  3.  

Here's a larger example that demonstrates what this does:

  1.  
  2. output_add_rewrite_var('auth', '412e11');
  3.  
  4. ?>
  5. <a href="link.php">Click Here</a>
  6. <form action="form.php" method="POST">
  7. <input type="submit" />

PHP propagates the auth token in both the link and the form:

  1. <form action="form.php" method="POST">
  2. <input type="hidden" name="auth" value="412e11" />
  3. <input type="submit" />