Bite My Code

the code with no tag

Archive for the ‘referer’ tag

Get your Sign Up Referer by PHP

without comments

Thanks to Google Analytics that help us on website traffic tracking. But for some reason we still need to track some specific statistics, like the referer of the user when user do a specific action, for a example, sign up on your website.

By using session in PHP, we can easily get the referer no matter which page the user is coming. Here is the PHP source code:


session_start();

if (empty($_SESSION[_referer'])) {
	$host = parse_url($_SERVER['HTTP_REFERER'],PHP_URL_HOST);
	if (empty($host)) {
		$_SESSION['_referer'] = 'DIRECT';
	} else {
		$_SESSION['_referer'] = $host;
	}
}

$referer = $_SESSION['_referer'];

And we can store the referer in the database or other tracking suit if we need.

You can download the php file and simply include it in your php header.

Source Download

How to use:
1. Download the php source file and rename to .php extension
2. Include the php file in your php source or in your generic header file
3. Now you can just use the $referer to get the referer host information


include '/path/to/referer.php';

echo $referer;

Enjoy!

Written by alvin

August 1st, 2008 at 6:37 am

Posted in Web Development

Tagged with , , ,