Page 1 of 1

Help with PHP Sessions and Referrer

Posted: Thu Nov 05, 2009 3:06 pm
by dremes
Hi I have been trying to show different telephone numbers per referrer through out all site pages, the script i have hashed together is below, it works and checks referrer then displays the correct number, how ever upon clicking internal links within my site the new referrer is then my site so it then shows the default.

There must be away to store and use the original referrer, then show a message accross all site pages quoting the referrer relevant number.

Many thanks to anyone that can help

<?PHP
session_start();
if (isset($_SESSION['referrer'])) {
$referrer = $_SESSION['referrer'];
} else {
$referrer = strtolower($_SERVER["HTTP_REFERER"]);
}
if (strstr($referer,"essential"))
{
echo("<h1>Telephone Number1</h1>");
}
else if (strstr($referer,"site2"))
{
echo("<h1>Telephone Number2</h1>");
}
else if (strstr($referer,"site3"))
{
echo("<h1>Telephone Number3</h1>");
}
else if (strstr($referer,"site4"))
{
echo("<h1>Telephone Number4</h1>");
}
else
{
echo("<h1>Telephone Number5</h1>");
} $_SESSION[’referrer’] = $referrer; // store session data
?>

Re: Help with PHP Sessions and Referrer

Posted: Thu Nov 05, 2009 3:16 pm
by AbraCadaver
You're setting the session var on every page load. I just moved that one line so that you only set the session var if it's not already set. Hope it works.

Code: Select all

session_start();
if (isset($_SESSION['referrer'])) {
$referrer = $_SESSION['referrer'];
} else {
$referrer = strtolower($_SERVER["HTTP_REFERER"]);
$_SESSION['referrer'] = $referrer; // store session data
}
if (strstr($referer,"essential"))
{
echo("<h1>Telephone Number1</h1>");
}
else if (strstr($referer,"site2"))
{
echo("<h1>Telephone Number2</h1>");
}
else if (strstr($referer,"site3"))
{
echo("<h1>Telephone Number3</h1>");
}
else if (strstr($referer,"site4"))
{
echo("<h1>Telephone Number4</h1>");
}
else
{
echo("<h1>Telephone Number5</h1>");
}

Re: Help with PHP Sessions and Referrer

Posted: Thu Nov 05, 2009 5:03 pm
by dremes
No unfortunately that has now stopped the number changing all together and the relevant referrer number does not show defaulting to the last else / echo

Any more thoughts welcome

Re: Help with PHP Sessions and Referrer

Posted: Thu Nov 05, 2009 5:37 pm
by AbraCadaver
dremes wrote:No unfortunately that has now stopped the number changing all together and the relevant referrer number does not show defaulting to the last else / echo

Any more thoughts welcome
Currently, if someone comes to your page from a referrer (another link) then it sets that as a session var. If then they click on internal links within your site it doesn't replace the session var. You either have the initial referrer or you have a new referrer every time you click a link, unless you can give more conditions that can be met.



-Shawn

Re: Help with PHP Sessions and Referrer

Posted: Thu Nov 05, 2009 5:42 pm
by AbraCadaver
For what I posted to work, you're going to have to clear your cookie and restart your browser since you already have a session var set from previous attempts. If not, then I guess I'm still fuzzy on what you want to happen.

-Shawn

Re: Help with PHP Sessions and Referrer

Posted: Thu Nov 05, 2009 5:42 pm
by dremes
Hi Shawn,

Many thanks for your help so far,

I am aiming to change the telephone number in the header of site depending upon where the referral came from

IF came from site A > Mysite Display Number 00001 through all my site pages
IF came from site B > Mysite Display Number 00002 through all my site pages
IF came from site c > Mysite Display Number 00003 through all my site pages

I can make this happen with the original script i posted but upon internal link clicking the new referrer becomes mysite there for fouls everything.

I am more then happy to send over some beer money to whom who resolves this one!!!

Re: Help with PHP Sessions and Referrer

Posted: Thu Nov 05, 2009 5:46 pm
by dremes
AbraCadaver wrote:For what I posted to work, you're going to have to clear your cookie and restart your browser since you already have a session var set from previous attempts. If not, then I guess I'm still fuzzy on what you want to happen.

-Shawn
cleared browser history inc cookies shall try again in different broswer