views:

350

answers:

2

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.

+2  A: 

You're almost there! -- just save the $referrer in the $_SESSION variable. Then you can check if $_SESSION['referrer'] is set on subsequent page requests and act accordingly.

Janek
Thanks Janek. I just realized what my initial problem was. I was getting this "Cannot modify Header Information" error. It was because I had whitespace between the first ?> and the second <? in my code.
Joel Marcey
+1  A: 

Why don't you store the referer in a session variable ? In your landing page you can have this :

<?php
session_start();
if(empty($_SESSION['comeFrom']) && !empty($_SERVER['HTTP_REFERER'])){
    $_SESSION['comeFrom'] = $_SERVER['HTTP_REFERER'];
}
?>

Then, in your home page :

<?php
session_start(); 
if(!empty($_SESSION['comeFrom']) &&
   preg_match("/referrerSite.com/",$_SESSION['comeFrom'])){
   header('Location: http://www.example.com/camefromad.html');
}
?>
Arkh
Excellent Arkh. Thanks! After fixing the whitespace problem I mentioned in my comment above, I applied this code which seems much cleaner than the route I was initially going and things seem to work nicely.
Joel Marcey