PHP Redirect Function

By  on  

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

This function is incredibly easy to use and saves a lot of code.

Recent Features

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

Incredible Demos

  • By
    Dynamic Waveform Visualizations with wavesurfer.js

    Waveform images are an awesome addition to boring audio widgets.  They can be functional as well as aesthetically pleasing, allowing users to navigate audio visually.  I recently found wavesurfer.js, an amazing waveform image utility that uses to Web Audio API to create super customizable...

  • By
    Image Reflections with CSS

    Image reflection is a great way to subtly spice up an image.  The first method of creating these reflections was baking them right into the images themselves.  Within the past few years, we've introduced JavaScript strategies and CANVAS alternatives to achieve image reflections without...

Discussion

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

  2. 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.

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

    Thanks.

  4. 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!

  5. Nice and thanx

  6. Mike Vysocka

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

  7. eskenfense

    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

  8. txongoku
  9. shayan

    hey,thats ok

    how i moust do if i want to run one of may actions in classes

    for example=>
    http://localhost:8007/frontend_dev.php/rmsSetupReservation/create

  10. Nika

    i get this errror message…what is the problem please help me…
    Warning: Cannot modify header information – headers already sent by (output started at C:\wamp\www\bv\bookinsert.php:1) in C:\wamp\www\bv\bookinsert.php on line 139

  11. If you have problems with header function :

    header("Location:www.google.com");

    you can use this line if you are fans of JavaScript ( client side ):

    echo "location.href='http://www.google.com'";

    OR , use this line if are not ( server side ):

    echo '';

    Sorry for my English :-)

  12. Werner

    You can run into trouble if you redirect using relative URLs. It should also be noted that
    RFC1945 requires a complete absolute URI for redirection, see: http://tools.ietf.org/html/rfc1945

  13. i have problem when i redirect the header location:-
    Warning: Cannot modify header information – headers already sent by (output started at /home/adnanakh/public_html/dcncaclub.org/admin/website_insert_back1.php:3) in /home/adnanakh/public_html/dcncaclub.org/admin/website_insert_back1.php on line 18

  14. Rajnish kumar singh

    What’s problem here. anybody can solve this problem.

    header('Location:list_books.php');
    exit();
    
    
    Warning
    : Cannot modify header information - headers already sent by (output started at D:\xamppp\xampp\htdocs\bootstrap\theme\admin\templete\nav.php:56) in
    D:\xamppp\xampp\htdocs\bootstrap\books\add_book_ctrl.php
    on line
    21
    
  15. It’s still working fine.

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

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!