Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

PHP Redirect Function

DZone's Guide to

PHP Redirect Function

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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();
}

Do you have any suggestions for improvement?

Take a look at the Indigo.Design sample applications to learn more about how apps are created with design to code software.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}