Commonly, a website has a menu bar with links to different pages of the site— Home, Page A, Page B, etc..
Let’s say you have a site with address http://www.example.com. 99% when someone types that URL into their browser, they are directed to the page represented by “Home”. It usually has the optional extension of index[.html|.php] (e.g., http://www.example.com/index.html). However, there are use cases where, depending from where a user is going to hit your website, that you want to redirect them to a different page. For example, if you have an ad on a site promoting your page, you might want to redirect them to: http://www.example.com/camefromad.html which will look pretty much the same as your normal “Home” page, but maybe with a little added text specifying that they came from the page where the ad was on.
My question is:
Is there a way, after a user has landed on the site from a referred page, to use PHP to ensure that clicking the “Home” link will always go to http://www.example.com/camefromad.html IF the user landed there to begin with? What I am trying to avoid is for a user to have come to http://www.example.com/camefromad.html and then click “Home” in the menu bar and end up at the default home page never to be able to return to the http://www.example.com/camefromad.html page (I don’t have that landing page enabled in the public menu since I don’t want the general user to be able to go there).
This is my first time using PHP and I am trying to piece it together using session state, etc., but I just haven't quite completed the puzzle yet. Here is what I have in index.php (which is the file that represents the "Home" link:
<?
session_start();
$_SESSION['amSet']= 0;
?>
<?
if ($_SESSION['amSet'] == 0) {
if (!empty($_SERVER['HTTP_REFERER'])) {
$referrer = $_SERVER['HTTP_REFERER'];
$_SESSION['amSet'] = 1;
}
}
if (preg_match("/other.example.net/",$referrer)) {
header('Location: http://example.com/camefromad.html');
}
?>
Basically, my thinking is to set a variable when the user comes to the site so that the referrer variable is set to that referring site. But if the user clicks around links in the site itself, I don't want the referrer reset. I want it to stay what it was when the user first came to the site, so if the user clicks on the "Home" link it will always refer the user to the appropriate page. Obviously, the code I have above is not working.