Help with PHP Sessions and Referrer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dremes
Forum Newbie
Posts: 4
Joined: Thu Nov 05, 2009 2:59 pm

Help with PHP Sessions and Referrer

Post 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
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with PHP Sessions and Referrer

Post 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>");
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dremes
Forum Newbie
Posts: 4
Joined: Thu Nov 05, 2009 2:59 pm

Re: Help with PHP Sessions and Referrer

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with PHP Sessions and Referrer

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with PHP Sessions and Referrer

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dremes
Forum Newbie
Posts: 4
Joined: Thu Nov 05, 2009 2:59 pm

Re: Help with PHP Sessions and Referrer

Post 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!!!
dremes
Forum Newbie
Posts: 4
Joined: Thu Nov 05, 2009 2:59 pm

Re: Help with PHP Sessions and Referrer

Post 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
Post Reply