Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2012, 04:58 PM   PM User | #1
jmcnulty
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
jmcnulty is an unknown quantity at this point
storing a session http_referer

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>'; }; ?>
jmcnulty is offline   Reply With Quote
Old 10-05-2012, 05:26 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Simply store it in the session and always check for if its set:
PHP Code:
<?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.
Fou-Lu is offline   Reply With Quote
Old 10-05-2012, 05:50 PM   PM User | #3
jmcnulty
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
jmcnulty is an unknown quantity at this point
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.
jmcnulty is offline   Reply With Quote
Old 10-05-2012, 06:04 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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 Code:
<?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 Code:
<?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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
jmcnulty (10-05-2012)
Old 10-05-2012, 07:28 PM   PM User | #5
jmcnulty
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
jmcnulty is an unknown quantity at this point
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 Code:
<?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 Code:
<?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.
jmcnulty is offline   Reply With Quote
Old 10-05-2012, 09:47 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by jmcnulty View Post
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 Code:
<?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 Code:
<?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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
http_referer, php, storing session

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:36 AM.


Advertisement
Log in to turn off these ads.