Using the PHP Header Function:


header ("Location: index.php");

Redirect To Previous Page For dynamic url:

You need session_start() at the very beginning of any page that will use session variables.


// SET REFERRER
session_start();
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}

function selfURL() {
if(!isset($_SERVER['REQUEST_URI'])) {
$serverrequri = $_SERVER['PHP_SELF'];
}
else {
$serverrequri = $_SERVER['REQUEST_URI'];
}
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$_SESSION['ref'] = $protocol."://>/".$_SERVER['SERVER_NAME'].$port.$serverrequri;
}

selfURL();

<?php echo $_SESSION['ref']; ?>

There you go. So now – all you do is place this in the head of your document and that current URL is will be stored as $_SESSION['ref'] – so then you only have to do this:


header("Location: " . $_SESSION['ref']);

If you post to a separate page, you would need to use the other method.
You could probably improve this to be just as useful if not more than gregs method.


header("Location: " . $_SERVER['HTTP_REFERRER']);

LEAVE A REPLY

Please enter your comment!
Please enter your name here

four × 5 =