This appears to be the same question asked earlier, but this time in PHP rather than javascript. And so I'll treat it as such in my answer.
You don't need to apply $_GET variables to links on your pages to do this. You can also check the referrer URL in PHP which will allow you to see which site the previous page was and thus you can determine what to do.
<?php
if(isset($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], "your-site.com") !== FALSE) {
echo '<a href="' . $_SERVER['HTTP_REFERER'] . '">Back</a>';
} else {
echo '<a href="http://www.your-site.com/general-page.php">Back</a>';
}
?>
I actually had the back button link to the actual previous url rather than having it set a link with 'history.go(-1)' because if you're going to use javascript links to navigate this way you can use the previous solution. This is designed to be a full PHP-based solution. And based on your code you can always set a '$link' variable instead of echoing out the <a> tag like I've done in my code here.