$needle) { if(stristr($file_name, $needle)) { $current_referrer=""; } } // ignore blacklisted domains foreach($uninteresting as $key => $needle) { if(in_array($needle, $host_bits)||in_array($needle, $uri_parts)) { $current_referrer=""; } } // remove index page names and any mess after them ### not good because of MT and dynamic pages ### (add .* at the end of the regex if this leaves too much mess) $current_referrer=eregi_replace("(index|default|home)\.(php|php3|htm|html|asp|cfm|shtml)", "", $current_referrer); // get rid of any stuff in the REQUEST_URI // $current_page=eregi_replace("\?.*", "", $current_page); // ################# enter your db info here ################### // connect to database $connect = mysql_pconnect("", "user", "pass") or die("Could not connect to mysql server."); mysql_select_db("db_name", $connect) or die ("Could not connect to database."); // check this isn't a reload or click-through abuse (if the last referral through this link came from the same ip, ignore it) // pick out dates and ips of previous entries for this referrer $query="select ip, datetime from referrers where referrer='$current_referrer'"; $request=mysql_query($query) or die("Testing: problem with $query"); $previous_datetime="0"; // check each date stamp against the previous ones to pick out the ip associated with the lastest referral while($row=mysql_fetch_array($request)) { $ip=$row["ip"]; $datetime=strtotime($row["datetime"]); if($datetime > $previous_datetime) { $last_ip=$ip; $previous_datetime=$datetime; } } // if the latest ip for this referrer is the same as the current one, trash this referrer as duplicate if($last_ip==$current_ip) { $current_referrer=""; } // what follows is identical, whether www or none // get rid of the www if possible, and grab the title of the page if($fp=@fopen($current_referrer, "r")) { while (!feof ($fp)) { $line = fgets ($fp, 1024); /* This only works if the title and its tags are on one line */ if(eregi ("(.*)", $line, $out)) { $title_long = $out[1]; // print("

Got the title for the long referrer, yay! $title_long

"); $referrer_title=$title_long; } $counter_long++; } // if there is no www, $short_referrer will be the same as $current_referrer $short_referrer=str_replace("www.", "", $current_referrer); if($short_referrer!==$current_referrer) { // if short_referrer and current referrer are identical, it's useless to do all this again if($fp_short=@fopen($short_referrer, "r")) { while (!feof ($fp_short)) { $line = fgets ($fp_short, 1024); /* This only works if the title and its tags are on one line */ if(eregi ("(.*)", $line, $out)) { $title_short = $out[1]; // print("

Got the title for the short referrer, yay! $title_short

"); } $counter_short++; } if(($counter_long==$counter_short)&&($title_long==$title_short)) { // we can do away with the www safely!! $use_www=false; } else { // print("

$short_referrer exists, but titles ($title_long, $title_short) or number of lines ($counter_long, $counter_short) do not match. Use www!

"); $use_www=true; } } else { // print("

$short_referrer does not exist.

"); $use_www=true; } } else { // print("

There wasn't even a www in the initial url!

"); $use_www=false; } } else { // print("

Initial uri $current_referrer does not exist.

"); // this gets rid of 404s and non-public pages $current_referrer=""; } // different behaviour, depending on if we need to use www or not if($use_www) { // we need to use the www // clean up database $query_ref="update referrers set referrer='$current_referrer' where referrer='$short_referrer'"; $query_tit="update titles set referrer='$current_referrer' where referrer='$short_referrer'"; mysql_query($query_ref) or die("Problem updating referrer table."); mysql_query($query_tit) or die("Problem updating titles table."); } else { // we can do away with www // clean up database $query_ref="update referrers set referrer='$short_referrer' where referrer='$current_referrer'"; $query_tit="update titles set referrer='$short_referrer' where referrer='$current_referrer'"; mysql_query($query_ref) or die("Problem updating referrer table."); mysql_query($query_tit) or die("Problem updating titles table."); $current_referrer=$short_referrer; } if(!empty($current_referrer)) { // we don't want to do this if we are dealing with google search terms if(!isset($search)) { $query="select title from titles where referrer='$current_referrer'"; $result=mysql_query($query) or die("Problem with $query."); if(mysql_num_rows($result)==0) { // ||mysql_result($result, 0)=="" stick the title of the referring page in the db, as it isn't there yet $referrer_title=str_replace("'", "\\'", $referrer_title); $query="insert into titles (referrer, title) values ('$current_referrer', '$referrer_title')"; mysql_query($query) or die("problem inserting title ($query)"); } } // stick this referrer in db $current_date=date("Y-m-d H:i:s"); $stick_query="insert into referrers (page, referrer, datetime, ip) values('$current_page', '$current_referrer', '$current_date', '$current_ip')"; mysql_query($stick_query) or die("Problem inserting referrer in database ($stick_query)"); } ?>