Thursday, January 8, 2009

Get referrer with PHP

Here is some code to find out the referrer to a page in PHP.  This can be very useful if you want to redirect back after login or something like that.  You can also set it up to make sure a visitor came to the page from within your site or something like that.

<?php
session_start();

if(!isset($_SESSION[’referrer’])){

//get the referrer
if ($_SERVER[’HTTP_REFERER’]){
$referrer = $_SERVER[’HTTP_REFERER’];
}else{
$referrer = “unknown”;

}
//save it in a session
$_SESSION[’referrer’] = $referrer; // store session data
}
?>


Hope this is useful.  The reference is: Get a referral URL from the session using PHP

No comments: