how to track referrer at session start?

Discussion in 'PHP' started by frankcow, Nov 22, 2006.

  1. #1
    I'd like to do the following:
    1. when a user enters the website, check their referrer
    2. store it in a session variable
    3. at any point when they convert in any way (contact form, eval form, etc), I store the referrer in a hidden field

    Any ideas? I know how to do this in ASP, with the session_onstart sub, but how do I do it with PHP?
     
    frankcow, Nov 22, 2006 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if($_SERVER['HTTP_REFERER']){
    $_SESSION['referer'] = $_SERVER['HTTP_REFERER'];
    }
     
    lbalance, Nov 22, 2006 IP
    frankcow likes this.
  3. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #3
    will this count only URLs from external domains as referrers?
     
    frankcow, Nov 22, 2006 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You could do something like:

    
    if ( ! isset( $_SESSION[ 'referer' ] ) ) {
        if ( $_SERVER[ 'HTTP_REFERER' ] ) {
            $_SESSION[ 'referer' ] = $_SERVER[ 'HTTP_REFERER' ];
        }
    }
    
    Code (markup):
    and that will set it only if it hasn't already been set for this session.
     
    TwistMyArm, Nov 22, 2006 IP
    frankcow likes this.
  5. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #5
    thank you. But will this track referrals from external domains only?
     
    frankcow, Nov 22, 2006 IP
  6. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it will refer internal links.

    my guess is that you should add a preg_match statement
    to weed out your domain name.
     
    lbalance, Nov 22, 2006 IP
  7. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #7
    am I wrong in assuming that it would only do this if someone came to the site directly, then clicked? In which case, could I not on first check just set the session variable to 'direct' if there is no http referrer?
     
    frankcow, Nov 22, 2006 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    
    $host = parse_url(str_replace('www.', null, $_SERVER['HTTP_HOST']));
    $referer = parse_url(str_replace('www.', null, $_SERVER['HTTP_REFERER']));
    
    if (isset($referer['host']) AND $referer['host'] != $host['host'])
    {
    	$_SESSION['referer'] = $_SERVER['HTTP_REFERER'];
    }
    
    PHP:
    Untested.
     
    nico_swd, Nov 23, 2006 IP
    frankcow likes this.
  9. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #9
    awesome, thanks everyone!
     
    frankcow, Nov 23, 2006 IP