Lets say you have a page x.php which is in the server and y.php which a particular user is comming from.
Code for y.php
<?php
<a href="x.php">go to x</a>
?>
Code for x.php
<?php
if(isset($_SERVER['HTTP_REFERER'])){//checking if user comming from other page or came here directly
$referer = $_SERVER['HTTP_REFERER'];
}
//now the $referer holds path of y page
//now you can use it to redirect
if(//some condition){
heder('Location:'.$referer);
}
?>