I wish to redirect users to the login page if they attempt to visit a page which requires them to be logged in. After logging in, however, I want to redirect the user back to their original destination. I've written a "redirect.php" script which is to be included on all such pages:
<?php
require "session.php";
if(!$user){
header("Location: login.php?dest=".urlencode($_SERVER["REQUEST_URI"]));
die();
}
?>
Then on my login page I have the following:
<?php
$dest = "./";
if(isset($_GET["dest"])){
$dest = $_GET["dest"];
}
?>
with the following JavaScript:
var URL = "<?php echo $dest; ?>";
//...
//upon successful login (via AJAX):
window.location.replace(URL);
Everything here works as intended but where does this stand from a security standpoint?
One vulnerability that comes to mind is something like
http://mysite.com/login.php?dest=http://phishingsite.com
How might I best prevent something like this? Would regex be suitable here?
Are there any other security concerns with this type of thing? Perhaps a standard way of doing this? Or better yet, a method which does not use GET variables at all?