About the Author

Chris Shiflett

Hi, I’m Chris: entrepreneur, community leader, husband, and father. I live and work in Boulder, CO.


PHP Magazine December Issue

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:

<?php 

output_add_rewrite_var
('auth''412e11');

?>

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

<?php 

output_add_rewrite_var
('auth''412e11');

?>
<a href="link.php">Click Here</a>
<form action="form.php" method="POST">
<input type="submit" />
</form>

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

<a href="link.php?auth=412e11">Click Here</a> 
<form action="form.php" method="POST">
<input type="hidden" name="auth" value="412e11" />
<input type="submit" />
</form>

About this post

PHP Magazine December Issue was posted on Fri, 02 Dec 2005. If you liked it, follow me on Twitter or share:

4 comments

1.Matthew Weier O'Phinney said:

I wish I'd seen that one earlier... I had to do something like that for a client last year, as they had some specific session handling stuff they were handing off to us that we needed to retain. I figured out a solution, but it wasn't optimal.

Nice pointer.

Fri, 02 Dec 2005 at 19:11:17 GMT Link


2.Kick The Donkey said:

Do you have be using output buffering to use that function? The documentation is unclear...

Sun, 04 Dec 2005 at 01:53:33 GMT Link


3.Ed Finkler said:

Donkey, it looks like there's an implicit ob_start when you call this function. Also note that this function only modifies URLs that don't contains a protocol://domain, so if you use "full" URLs within your app, this won't work.

Sun, 04 Dec 2005 at 05:13:55 GMT Link


4.Karen said:

To avoid the "resend" message on forms when using the browser's back button, I use a separate page to do the processing and then redirect using Location headers. When I use the output_add_rewrite_var, it doesn't effect the URL passed to the header. I just have my redir function add it on. Is there a better way?

Sat, 18 Apr 2009 at 04:30:08 GMT Link


Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.