Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

PHP Redirect Function

8 Responses »

After form submission or a page redirect is triggered, it's commonplace to redirect the user to a different page or to the same page, formatted in a different way. Usually, you'd complete this by coding:

header('Location:  destination.php');
exit();

This is a completely acceptable way to code your pages, but I prefer to use a redirect function instead. Why? It's much more readable, and quite honestly, I'm tired of writing the header('Location: ...') code.

function redirect($url,$permanent = false)
{
	if($permanent)
	{
		header('HTTP/1.1 301 Moved Permanently');
	}
	header('Location: '.$url);
	exit();
}

Discussion

  1. December 28, 2007 @ 4:11 pm

    Do you use a plugin to highlight and format your php code?

  2. December 28, 2007 @ 5:22 pm
  3. December 28, 2007 @ 10:02 pm

    Shawn’s correct — you can go to the URL above to download the javascript and CSS files. I’m not using a WordPress plugin though — it’s easy to install on your own.

  4. December 29, 2007 @ 1:43 am

    Cool, I like the features it has over my current solution that I am using.

    Thanks.

  5. August 8, 2008 @ 11:53 am

    Ahm… As you’re already talking about the syntaxhighlighter… Why is the little “copy code”-snippet copying code AFTER it says “the code is in your clipboard now”!? Wouldn’t it make sense to copy it before the JS alert?
    Well, anyway – nice website Dave! I like it – and all the little code snippets are really nice. So simple, but helping a lot. Sometimes it’s harder to create them “the easy way”, so this really comes in handy. Thanks!

  6. August 9, 2009 @ 7:53 pm

    Nice and thanx

  7. mike vysocka
    December 14, 2009 @ 1:44 pm

    You can use highlight_string() in PHP to hightligt your code, or highlight_file()…
    http://php.net/manual/en/function.highlight-string.php

  8. eskenfense
    January 3, 2010 @ 5:29 am

    when i use this–>

    it respondes like
    Warning: Cannot modify header information – headers already sent by (output started at C:\wamp\www\New2\4.php:1) in C:\wamp\www\New2\4.php on line 3

    what shall i do

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!