...

View Full Version : PHP Display Telephone Number On Referrer



dremes
11-05-2009, 04:28 PM
I have used the php below to show a different telephone number in the header of the site depending upon where the visitor comes from. The code below works but looses the original referrer information upon click of own site links, defaulting the telephone number back to the generic number.

Is there away to save it to the session, maybe the start session and save session is not done correct?

Thanks for any help

.............................................................................

<?PHP
session_start();
if(!isset($_SESSION[’referrer’])){

$referer = strtolower($_SERVER["HTTP_REFERER"]);

if (strstr($referer,"site1"))
{
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
}?>

Fumigator
11-05-2009, 05:44 PM
You're spelling your $referrer variable two different ways: $referer, and $referrer.

You should also keep this little factoid in mind (copied from the PHP manual):


'HTTP_REFERER': The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

dremes
11-05-2009, 06:20 PM
for some reason when i write referrer all with the same spelling upon clicking on internal links all referrer telephone numbers are lost.

I know HTTP Referrer is not that reliable, but what else could be considered I looked at java script referrer but think that is less reliable then php referrer.

Please if you could change the code to the way you think it should be and repost it, maybe its just me not been able to spell!

Many thanks for your help

Fumigator
11-05-2009, 06:21 PM
And you have session_start() at the top of every page?

dremes
11-05-2009, 06:37 PM
yes as the php is in the template of the header, so the whole script would be repeated on everypage.

Fumigator
11-05-2009, 06:44 PM
Oh-- look at your logic, I just noticed that you don't do anything if $_SESSION['referrer'] is set. Don't you want to use that value if it's set?

dremes
11-05-2009, 06:47 PM
Yes i do want to use it to display the referrer number, I need to call back what was set in the session, but how??

thanks

Fumigator
11-05-2009, 07:04 PM
Assign value to $referrer based on whether $_SESSION['referrer'] is set.



if (isset($_SESSION['referrer'])) {
$referrer = $_SESSION['referrer'];
} else {
$referrer = strtolower($_SERVER["HTTP_REFERER"]);
}

dremes
11-05-2009, 07:20 PM
looks like this now --- but only the last else echo tel5 shows from all referrers. If theres a fix you could help with I am more then happy to send you on some form of payment for your efforts,, thanks

<?PHP
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
}?>

Fumigator
11-05-2009, 07:39 PM
Don't you want to be running through all that code to echo the <h1> tag regardless of the results of your first "if" statement?

dremes
11-05-2009, 07:51 PM
Yes i need to echo the correct telephone number h1 tag based on original referrer which is stored in the session...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum