...

View Full Version : storing a session http_referer



jmcnulty
10-05-2012, 04:58 PM
hi

I am trying to get the phone number to change at the top of my site dependent on where the visitor comes from...

The code works when the user initially lands on the page - but when the user navigates throughout the rest of the site the number changes back to the regular number again...

I know I somehow need to store a session - but I don't know how to do that correctly - I also don't know what I need to put on the headers of other pages to correspond correctly - I imagine its not the same code for the other pages because I would have to just store the session not start a new one each time.

HELP PLEASE!

<?php $ref=getenv('HTTP_REFERER');
if (strpos($ref,"wherever.com")>0) { echo '<span class="phonenumber">888-111-1234</span>'; }
else { echo '<span class="phonenumber">888-555-6789</span>'; }; ?>

Fou-Lu
10-05-2012, 05:26 PM
Simply store it in the session and always check for if its set:


<?php
session_start();
if (!isset($_SESSION['ref']))
{
$_SESSION['ref'] = getenv('HTTP_REFERER'); // or $_SERVER['HTTP_REFERER'] would work
}

// Do your checks here.

So long as that's included in any page that needs to check, it should carry that over.
The only thing to note is that there is no guarantee that HTTP_REFERER will ever be available. That's completely up to the client if they want to provide that to you or not.

jmcnulty
10-05-2012, 05:50 PM
thanks for responding so quickly - (I'm fairly new to this) so if I understand correctly my code would look like this on my home page - which is where the referrer would be coming from in my case:

<?php
session_start();
if (!isset($_SESSION['ref']))
{
$_SESSION['ref'] = getenv('HTTP_REFERER'); // or $_SERVER['HTTP_REFERER'] would work
}

// Do your checks here.

if (strpos($ref,"wherever.com")>0) { echo '<span class="phonenumber">888-111-1234</span>'; }
else { echo '<span class="phonenumber">888-555-6789</span>'; };
?>


Then on the other pages (which have different headers) what would the corresponding code look like?

Same as above?

I just need it to understand 'ok we know who the referrer is now and so we are going to keep the changed number throughout the site'

I think my big problem is that I am navigating to a new header almost everytime and not the same header.

Fou-Lu
10-05-2012, 06:04 PM
Well, there's a few approaches you can take. If you store the referrer as I have in the session, then you need to evaluate it every time. That would make this go into a script that every other script will include, such as a global script.
If you just need the number, then that can be done as well:


<?php

session_start();
if (!isset($_SESSION['phonenumber']))
{
$ref = getenv('HTTP_REFERER');
if (strpos($ref, "wherever.com") !== false)
{
$_SESSION['phonenumber'] = '888-111-1234';
}
else
{
$_SESSION['phonenumber'] = '888-555-6789';
}
}


Now wherever you need it, you can simply:


<?php

session_start();
printf('<span class="phonenumber">%s</span>', $_SESSION['phonenumber']);


I personally do not like this though as in order to verify that session already has a phone number would require me to perform an isset check on the phonenumber anyway. With either approach, I'd suggest making it in a global script but storing the variable instead of printing it directly. That way you can use it when and where you want instead.

jmcnulty
10-05-2012, 07:28 PM
ok it's definitely getting closer. Here is my hopeful final issue.

just like you had mentioned embed this code at the very top of my primary page:

CODE A

<?php session_start(); if (!isset($_SESSION['phonenumber'])) { $ref = getenv('HTTP_REFERER'); if (strpos($ref, "localprice.com") !== false)
{
$_SESSION['phonenumber'] = '888-540-9909';
}
else
{
$_SESSION['phonenumber'] = '888-646-0860';
}
}
?>

Then call upon it somewhere on the page:

CODE B

<?php session_start(); printf('<span class="phonenumbertext">%s</span>', $_SESSION['phonenumber']);?>


This works absolutely perfectly.

My issue now is - when navigating away from this page into my internal page - I have different header files - so when I use (Code B) it doesn't know what it is referencing.

On the flip side if I try to embed the first block of code (Code A) onto the other header files but this does not work because all traffic is coming through the home page - so the referring page is my own domain, the home page.

Is there a way I can put some code similiar to (Code A) on my internal pages that says 'ignore my own domain as the referer'

I apologize for the complete hassle with this but it's honestly just going over my head.

It's so very very close.

Fou-Lu
10-05-2012, 09:47 PM
ok it's definitely getting closer. Here is my hopeful final issue.

just like you had mentioned embed this code at the very top of my primary page:

CODE A

<?php session_start(); if (!isset($_SESSION['phonenumber'])) { $ref = getenv('HTTP_REFERER'); if (strpos($ref, "localprice.com") !== false)
{
$_SESSION['phonenumber'] = '888-540-9909';
}
else
{
$_SESSION['phonenumber'] = '888-646-0860';
}
}
?>

Then call upon it somewhere on the page:

CODE B

<?php session_start(); printf('<span class="phonenumbertext">%s</span>', $_SESSION['phonenumber']);?>


This works absolutely perfectly.

My issue now is - when navigating away from this page into my internal page - I have different header files - so when I use (Code B) it doesn't know what it is referencing.

On the flip side if I try to embed the first block of code (Code A) onto the other header files but this does not work because all traffic is coming through the home page - so the referring page is my own domain, the home page.

Is there a way I can put some code similiar to (Code A) on my internal pages that says 'ignore my own domain as the referer'

I apologize for the complete hassle with this but it's honestly just going over my head.

It's so very very close.

session_start() has to be added to any script that intends to use it. It was pointed out not long ago to me that calling it multiple times will raise a notice, so simply put it in a global script to prevent inclusion conflict. So long as sessions are being passed, it shouldn't attempt to extract it again. It does also mean that if a client leaves your site, then comes back through a different route, the number won't change if it was done in less than about 1440 seconds and the garbage collector grabs the session.

You can detect your site using $_SERVER['SERVER_NAME']. That could be useful to see if the referrer has changed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum