The PHP website created by its users!
Home
»
PHP » Redirect Plus
|
Redirect Plus
| Author | Drew |
| Description | Given a $relocate page, itll send the headers to redirect. Make sure you don't output any html or whitespace prior to this. |
| Rating | (11 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!
?>
Comments
No comments posted yet.
|