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?
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.
it will refer internal links. my guess is that you should add a preg_match statement to weed out your domain name.
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?
$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.