| [ PHPXref.com ] | [ Generated: Sun Jul 20 16:35:25 2008 ] | [ bBlog 0.7.6 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php // bBlog Plugin : referers. 2 // function.referers.php - Show ( and log ) recent referers 3 // based on code by nathan@ncyoung.com 4 // usage: 5 // in a page template : {referers}. 6 // To seperate links other than the default <br> : {referers sep=" | "} to seperate by pipe 7 // other paramaters : 8 // {referers pages="all"} show referers for all pages. 9 // ( the default is to just show the referers for the current page } 10 // 11 function identify_function_referers () { 12 $help = ' 13 <p>Referers is a Smarty function to be used in templates that captures and lists referers to the current page. 14 <p>Example usage ( in a template put ) : {referers} 15 <p>The referer link list is seperate by a <br> by default. To override :<br> 16 {referers sep=", "} to seperate by a commar. 17 <p>Other paramaters :<br> 18 {referers num=10} - show 10 referers. Default is 5<br> 19 {referers top=TRUE} - show top referes instead of newest<br> 20 {referers global=TRUE} - show referers for all pages, not just the current one<br> 21 Those paramaters may be combined : {referers num=10 global=TRUE sep=" | "}'; 22 23 24 return array ( 25 'name' =>'referers', 26 'type' =>'function', 27 'nicename' =>'Referers', 28 'description' =>'Captures and list referers', 29 'authors' =>'nathan@ncyoung.com', 30 'licence' =>'Free', 31 'help' => $help 32 ); 33 34 35 } 36 /* if you want to use this plugin out side of smarty, 37 use this mysql table format : 38 39 CREATE TABLE `bB_referers` ( 40 `visitID` int(11) NOT NULL auto_increment, 41 `visitTime` timestamp(14) NOT NULL, 42 `visitURL` char(250) default NULL, 43 `referingURL` char(250) default NULL, 44 `baseDomain` char(250) default NULL, 45 PRIMARY KEY (`visitID`) 46 ) TYPE=MyISAM; 47 48 and define your base url : 49 define('BLOGURL','http://www.example.com/'); 50 */ 51 define('T_REFERERS',TBL_PREFIX.'referers'); 52 53 logReferer(); 54 55 function smarty_function_referers($params, &$bBlog) { 56 $num = 5; 57 $sep = "<br />"; 58 $mode = "break"; 59 $global = ""; 60 $top = FALSE; 61 extract($params); 62 63 if($top) { $ret=toprefererlist($num,$global); } 64 else { $ret=refererlist($num,$global); } 65 66 if($mode = 'list') { return tolist($ret); } 67 else { return implode($sep, $ret); } 68 } 69 70 71 function tolist($array) 72 { 73 $ret="<ul>"; 74 75 foreach ($array as $elem) 76 { 77 $ret .= "<li>".$elem."</li>"; 78 } 79 80 $ret.="</ul>"; 81 82 return $ret; 83 } 84 85 86 //problems? suggestions? email the author! 87 // 88 // nathan@ncyoung.com 89 90 //get most linked to pages on site 91 //select count(visitURL) as count, visitURL from referer_visitLog group by visitURL order by count desc 92 93 // no need for this as we are already connected 94 // mysql_connect("dbHost", "dbUser", "dbPass"); 95 // mysql_select_db("dbName"); 96 97 //if ($refererList){ 98 // $ar = refererList($refererList,"global"); 99 // print join("<BR>",$ar); 100 //} 101 //if ($topRefererList){ 102 // print join("<BR>",topRefererList($topRefererList,"global")); 103 //} 104 105 function logReferer(){ 106 107 108 $currentURL = $_SERVER['REQUEST_URI']; 109 $fullCurrentURL = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 110 111 $ref = getenv('HTTP_REFERER'); 112 113 if (!$ref){ 114 dbg("no referer"); 115 return; 116 } 117 118 if ($ref != strip_tags($ref)){ 119 //then they have tried something funny, 120 //putting HTML or PHP into the HTTP_REFERER 121 dbg("bad char in referer"); 122 return; 123 } 124 125 $ignore = Array( 126 BLOGURL, 127 'http://www.myelin.co.nz/ecosystem/bot.php', 128 'http://radio.xmlstoragesystem.com/rcsPublic/', 129 'http://blogdex.media.mit.edu//', 130 'http://subhonker6.userland.com/rcsPublic/', 131 'http://subhonker7.userland.com/rcsPublic/', 132 'mastadonte.com', 133 '+++++++++++++' 134 135 ); 136 foreach ($ignore as $site){ 137 if (stristr($ref, $site)){ 138 dbg("referer ignored"); 139 return; 140 } 141 } 142 143 $doubleCheckReferers = 0; 144 145 if ($doubleCheckReferers){ 146 147 dbg("loading referering page"); 148 149 //this is so that the page up until the call to 150 //logReferer will get shown before it tries to check 151 //back against the refering URL. 152 flush(); 153 154 $goodReferer = 0; 155 $fp = @fopen ($ref, "r"); 156 if ($fp){ 157 //timeout after 5 seconds 158 socket_set_timeout($fp, 5); 159 while (!feof ($fp)) { 160 $page .= trim(fgets($fp)); 161 } 162 if (strstr($page,$fullCurrentURL)){ 163 dbg("found current url in page"); 164 $goodReferer = 1; 165 } 166 } 167 168 if(!$goodReferer){ 169 dbg("did not find \n\n:$fullCurrentURL:\n in \n\n\n :$page: \n\n\n"); 170 return; 171 } 172 173 } 174 175 176 $anchor = preg_replace("/http:\/\//i", "", $ref); 177 $anchor = preg_replace("/^www\./i", "", $anchor); 178 $anchor = preg_replace("/\/.*/i", "", $anchor); 179 180 181 $sql ="insert into ".T_REFERERS." (referingURL,baseDomain,visitURL) values ('$ref','$anchor','$currentURL')"; 182 183 //print $sql; 184 185 mysql_query($sql); 186 187 } 188 189 190 191 function refererList ($howMany=5,$visitURL=""){ 192 193 $i=2; 194 195 $ret = Array(); 196 197 //if no visitURL, will show links to current page. 198 //if url given, will show links to that page. 199 //if url="global" will show links to all pages 200 if (!$visitURL){ 201 202 $visitURL = $_SERVER['REQUEST_URI']; 203 204 } 205 206 if ($visitURL == "global"){ 207 $sqr_recentReferer = mysql_query("select * from ".T_REFERERS." order by visitID desc"); 208 } 209 else { 210 $sqr_recentReferer = mysql_query("select * from ".T_REFERERS." where visitURL = '$visitURL' order by visitID desc"); 211 } 212 213 214 215 while($result_row = mysql_fetch_array($sqr_recentReferer)){ 216 217 $fullUrl = $result_row['referingURL']; 218 $domain = $result_row['baseDomain']; 219 if (!$domain){ 220 continue; 221 } 222 223 if ($last[$domain]){ 224 continue; 225 } 226 $last[$domain] = 1; 227 228 // Fix parameter lists in URL, now conforms to xhtml 229 $fullUrl = str_replace("&", "&", $fullUrl); 230 231 $temp = "<a href=\"$fullUrl\">$domain</a>"; 232 233 array_push($ret,$temp); 234 235 if ($i++ > $howMany){ 236 break; 237 } 238 239 } 240 return $ret; 241 } 242 243 244 function topRefererList ($howMany=5,$visitURL=""){ 245 246 247 $i=2; 248 249 $ret = Array(); 250 251 252 //see refererList() for notes. 253 if (!$visitURL){ 254 $visitURL = $_SERVER['REQUEST_URI']; 255 } 256 257 if ($visitURL == "global"){ 258 $sqr_recentReferer = mysql_query("select Count(baseDomain) as totalHits, baseDomain from ".T_REFERERS." group by baseDomain order by totalHits desc limit $howMany"); 259 } 260 else { 261 $sqr_recentReferer = mysql_query("select Count(baseDomain) as totalHits, baseDomain from ".T_REFERERS." where visitURL = '$visitURL' group by baseDomain order by totalHits desc limit $howMany"); 262 } 263 264 while($result_row = mysql_fetch_array($sqr_recentReferer)){ 265 266 $count = $result_row['totalHits']; 267 $domain = $result_row['baseDomain']; 268 269 $uSet = mysql_query("select * from ".T_REFERERS." where baseDomain = '$domain' order by visitID desc"); 270 $uRow = mysql_fetch_array($uSet); 271 $latestUrl = $uRow["referingURL"]; 272 273 // Fix parameter lists in URL, now conforms to xhtml 274 $latestUrl = str_replace("&", "&", $latestUrl); 275 276 $temp = "<a href=\"$latestUrl\">$domain</a> ($count)"; 277 array_push($ret,$temp); 278 279 if ($i++ > $howMany){ 280 break; 281 } 282 283 } 284 return $ret; 285 } 286 287 function dbg($string){ 288 // global $bBlog; 289 // 290 } 291 292 293 294 295 296 297 /* 298 299 Usage: 300 301 You must include the library in order to use it. Issue the include statement once on each page in which you want to use this library, before you call any of the functions. A typical include statement would be: 302 303 include("refererLib.php"); 304 305 To log the referers visiting a given page, place this code on the page: 306 307 logReferer(); 308 309 310 To show a list of 5 pages that link to the current page (ordered by most recent visit) place this code: 311 312 $list = refererList(5); 313 foreach ($list as $link){ 314 print "$link<BR>"; 315 } 316 317 To show a list of the outside links most commonly used to get to the current page: 318 319 $list = topRefererList(5); 320 foreach ($list as $link){ 321 print "$link<BR>"; 322 } 323 324 In both cases, you can ask for a global list, i.e. a list of recent or top referers for all pages on your site that log referers: 325 326 $list = refererList(5,"global"); 327 foreach ($list as $link){ 328 print "$link<BR>"; 329 } 330 331 Or: 332 333 $list = topRefererList(5,"global"); 334 foreach ($list as $link){ 335 print "$link<BR>"; 336 } 337 338 */ 339 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |