PHPFront

Home > PHP > Redirect Plus
The PHP website created by its users!



Redirect Plus


AuthorDrew
DescriptionGiven a $relocate page, itll send the headers to redirect. Make sure you don't output any html or whitespace prior to this.
RatingThis script has been rated 4/5 (8 votes)

PHP Code


// the $error and $success parameters get passed via $_REQUEST/$_GET... you can parse them to tell your users the reason for redirection
 function redirect($page, $error = "", $success = "")
                {
                        $error_str = "";
                        $success_str = "";
                        $extra = "";
                        if(!empty($error))
                                $error_str = "?e=" . urlencode($error);
                        if(!empty($success))
                                $success_str = "?s=" . urlencode($success);
                        if(@$_REQUEST['force'] == 1)
                                $extra = "&force=1";

                        $relocate = "Location: " . $page . $error_str . $success_str;
                        $relocate = str_replace("&?e","&e", $relocate);
                        $relocate = str_replace("&?s","&s", $relocate);
                        header($relocate . $extra);
                        return;
                }

// In order to call this function, do something like this

<?php
          redirect
("index.php","This is an error message","This is a success message, you dont need both or even one for that matter");
          return; 
// make sure you have this in case the redirect is within an if() statement or else the rest of your code below the redirect will execute!

?>



Rate it: Print Print

Comments


No comments posted yet.

Name
Website
Your comment