Home > Programming > Referer – Wordpress Plugin

Referer – Wordpress Plugin

Warning: This plug-in is now unsupported. I haven’t had time to develop it for a while, and I can’t see that changing any time soon. If anybody wishes to do something with this then by all means be my guest.

Description »

Referer [sic] is a plugin for the popular blogging software WordPress. It allows you to display information about refers you have had from other websites. Currently available stats are Top URLs, Top Hosts Top Search Keywords, and Top Search Phrases. Basically these show how many people have clicked through from a given website and what people are searching for to find your website.

You can see this plugin in action at the bottom of my right-hand sidebar (see http://kitty0.org/blog/).

I know it’s spelt wrong but somehow that is the spelling used in the HTTP/1.1 Specification (RFC 2616) so I guess it is now the official spelling when describing “the address (URI) of the resource from which the Request-URI was obtained.”


Copyright / Disclamer »

Copyright (C) 2006 Hamish Morgan (referer [at] kitty0 [dot] org)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

FURTHER MORE this plugin may well corrupt your website, empty your database, delete the internet, run away with your girlfriend, instigate World War III, email your porn collection to your Mum, and perform satanic rituals to further the coming of Demonic Minions and general Armageddon. USE IT AT YOUR OWN RISK! You have been warned.

Design Goals »

  • Display a list of Urls and Hosts on a Wordpress blog to show where people have clicked though from to find the site.
  • Display a list of Search Words and Phrases that where used in a search to find the site.
  • Allow this display to be easily customized without the need to edit any files.
  • Log the referering url every time a page is loaded.
  • To quickly fill out the logged data with data recorded previously by allowing a webserver access log to be imported.
  • Provide some means by which results can be filtered out by URL, Host, or Search Word to counter the increase is referer spam.
  • Allow the site owner to browse and manage logged data in detail.



Download »

This plugin is in very early Alpha version. It has several bugs that I am aware of and probably many others that I am not. I distribute it in the hope that it will be useful, but WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY or FIRNESS FOR A PARTICULAR PURPOSE. It may well corrupt your website, empty your database, delete the internet and run away with your girlfriend. Use it at your own risk!

Click here to download.

The latest version is 0.41 but if you wish you may download an earlier version:

Installation »

  1. Extract the contents of the archive to your wp-content/plugins/ directory, creating a new directory: wp-content/plugins/referer/
  2. Make sure all these files are readable by the webserver. (See http://codex.wordpress.org/Changing_File_Permissionsread)
  3. Go to the “WordPress Admin -> Plugins” panel. Find the Referer plugin in the list and click “Enable” in the right-hand column.
  4. Go to “WordPress Admin -> Options -> Referer” and click Install. You should now see the plugins Options page.
  5. Add referer_topReferers() function to your template. This will display referer stats on your blog. If you have a custom template I can’t describe exactly what you should do but here is what you should do if you are using the WordPress default template.
    1. If your template files are writable on the server then login to WordPress Site Admin and go to Presentation -> Theme Editor. Otherwise you will have to edit the file on you computer and upload it to the correct location on your server.
    2. In Theme Editor click “Sidebar”. Or localy open /wp-content/themes/default/sidebar.php.
    3. Insert ” <li> <?php referer_topReferers(); ?> </li> ” at the bottom of the page just before the ” </ul> </div> ” tags. It should look something like this:
      <li>
      <?php referer_topReferers(); ?>
      </li>
      </ul>
      </div>
      
    4. Click “Upload File” to save your changes. Or locally save the file and upload it.
  6. Done! Browse to your blogs front page and you should see the Top Referers stats box in the bottom right-hand side of your side-bar.

Upgrading »

  1. Extract the contents of the archive to your wp-content/plugins/ directory, creating a new directory: wp-content/plugins/referer/
  2. Go to “WordPress Admin” -> “Options” -> “Referer” and click “Migrate”.
  3. If you are upgrading from version 0.10 you will need to configure your template as described in Installation Item 5.Download and extract the latest version of this plugin (above.)



Problems (Help!) »

So it isn’t working as expected. Well first go and read the last paragraph of the Copyright / Disclaimer (above) …and your back. See I told you so.

If you would like my help with a problem then, to speed up resolution, please:

  • Insure you are using the latest version of this plugin.
  • Tell me about your server: What version of PHP, MySQL and WordPress you are using? Is it Apache on Linux, or IIS on Windows?
  • Explain step-by-step exactly what happened up until the problem occurred.
  • List all your installed Wordpress plugins with version.
  • Provide a link to your blog and, if possible, to the page with the error.

Related Links »

Change Log »

Version 0.4 -> 0.41

  • Fixed call-time pass-by-reference bug that showed under some PHP5 configurations.

Version 0.3 -> 0.4

  • Split the plugin into many smaller files because ~250k of code in one file is just silly.
  • Put all the css into a seperate style sheet.
  • Added Manage options subpage which allows you to view and manipulate stored referer records.
  • Moved Log upload controls and other operations to Tools options subpage.
  • Removed %n% tag from the page template description because it isn’t very important.
  • Added DHTML Date/Time Selector by dynarch.com to Manage subpage.
  • Fixed bug where rows from imported log files would use the current date not the logged date.
  • Simplified default display templates, because they where confusing and where cause problems with IE (suprise suprise)
  • Complete re-write of log import functionality. Now has the ability to set any valid CustomLog directive to parse the log by.
  • HTTP Referer is processed after the page has been outputed to the client so it doesn’t slow the page load time.
  • Fixed bug where not all SQL was escaped properly.
  • Now Strips cache:llO61WBwk4wJ:example.org/ strings from search queries.
  • Default value of Blacklist Search Hosts option is now off.
  • Added AOL Search to default Search Engines list.

Version 0.21 -> 0.3

  • Added rel=”nofollow” to all generated links, so referer spammers dont get any pagerank for their trouble.
  • Added “Clear Logged Data” button to empty the referers data table.
  • Added GZip (.gz) compressed log file upload. (Not .tar.gz though.)
  • Added error checking for client-side form post failures.
  • Added disable on submit to options forms so it doesn’t get borked by users epileptic-clicking.
  • Added ability to import logs from a server path.
  • Hitting [Enter] key in the Options page now runs “Update Options” not “Restore Defaults”.
  • Max-upload in label for “Log File Upload” is now in human readable units.
  • Simplified the default display templates.
  • Fixed some HTML bugs in Options page.
  • Fixed bug where limit argument in topReferers() functions doesn’t update %*-limit% tag.
  • Fixed multilingual character support in search words and phrases.
  • Fixed bug where uploading files larger than max upload file size sometimes just reloads the page with no error.

Version 0.2 –> 0.21

  • Fixed bug where Host Blacklist didn’t blocks URLs.
  • Fixed/Enabled Searchengine Host Blacklisting
  • Improved default output format templates.
  • Added %snip% element to output format.
  • Fixed problem where stats titles could say “Top 0 Referers”
  • Fixed problem with submitting forms containing HTML entities (eg )
  • Fixed typo in function referer_fetchPhrases()
  • Function parseReferer now returns an associative array of referer info.
  • Add migration supportto make upgrading easier.
  • Fixed bug where empty lines in searchengine RegEx caused an error.

Version 0.10 –> 0.20

  • Added Search Phrases.
  • Added complete URL logging.
  • Changed database structure to use one table for all data.
  • Added search word blacklist
  • Improved default format strings for all data types.
  • Improved configuration options sub-panel layout.
  • Re-wrote almost everything to better accommodate new and upcoming features.
  • Removed all fatal errors.. so if something goes wrong it should at least still be possible to disable the plugin.

Known Bugs

  • In KHTML (Konqueror / Safari) does strange things with <abbr> tags.
  • Long strings in the Manage page are broken up by font-size:0 spaces so copy/paste doesn’t work properly. Additionaly it is malformed XHTML because empty tags should be stripped. Alternative solutions like <wb />, ­ and Unicode U+200B (zero width space), or normal spaces have their own cavetes.
  • In Manage page Search Words and Phrases are wrapped even if it is wraping on a whitespace. Very minor but anoying.
  • Not exactly a bug but I don’t have access to IE so no doubt everything gets garbled by that poor excuse for a browser.
  • Importing a malformed log file can throw many, many errors.
  • In Options that Display Format Templates are sometimes reported to have been saved even if no change has been made. This is due to varying new line encoding on different platforms.
  • Manage Admin page style is inconsistent with rest of Admin.

Roadmap (TODO)

  • Implement a “Reprocess logged data” tool to update data against new filters and blacklists.
  • Rewrite all string/regex functions to be mulltibyte safe.
  • Fuzzy match domain names in URLs (e.g www.example.com and example.com should be grouped.)
  • Improved and easier searchengine matching. Possibily defined as a host and a query identifier. e.g: For google: host=”/www.google..+/search/”, query=”/[?&]q=([^&])&/”
  • Or better yet host=”www.google”, query=”q” but the problem with the latter is it doesn’t cope with dogpile style urls: “search/blah/-/-/-/”
  • Log local searches
  • Add many more search engines. E.g: excite, hotbot, dmoz, aol, a9, technorati, altavista, images.google, dogpile, beta.search.msn, lycos, metacrawler, alltheweb, vivisimo, wisenut, gigablast, mamma, looksmart, search. netscape, clusty, about.com, iwom.ask, webcrawler.
  • Add “Clear” button to filters in Manage page.
  • Replace the error handling class with something less um… crap.
  • For log parsing use ApacheLogRegex::parse_n() to improve performance.
  • Add tooltips to result hheaders and clickable results in Manage page to give some hint as to what will happen if clicked.
  • Search words and phrases sometimes contain erroneous punctuation.
  • Add ability to delete (not just hide) records from the manage page.
  • Clarify deffinition of hiding, deletion, and blacklisting (when logging or when displaying) And alter respective controls.
  • Install and Mirgate should happen automatically
  • Referers page template (like links template)
  • Localization / I10n
  • Run as a Wordpress Widget
  • Current HTTP_REFERER Referer search word highlighting.

API Function Reference »

The following functions can be used in your template files to include referal statistics.

string referer_topReferers([integer limit [, boolean echo]])

This is a wrappers for a function in the Referer class. Use it in your  template to print a formated listst of top referers by catagory. This will  display all usefull information in a box and should be the only function you  have to include in your template. However if you wish to display only certain  parts of the data or format it yourself use the referer_top*() and  referer_fetch*() function below. They take the results of the all the other top*(function) functions (see below) and format it using the "referer" XHTMl snippets in the plugin options panel. It then outputs all the data into the page.

Optional paramater $limit determins tha maximum number of results to return. If it is not given then it will return the number set in the plugin options panel. If limit is given as 0 (zero) the ALL results will be returned.

If you set option paramater $echo as FALSE then instead of print the results  they will be returned so you can perform additional formatting or concatonate  them with other data. 

string referer_topUrls([integer limit [, boolean echo]])
string referer_topHosts([integer limit [, boolean echo]])
string referer_topWords([integer limit [, boolean echo]])
string referer_topPhrases([integer limit [, boolean echo]]) 

These are wrappers for function in the Referer class. Use them in you template print a formated lists of top referers.

They take the results of the fetch*() functions (see below) and format it using the XHTMl snippets in the plugin options panel. Then they output all the data into the page. 

Optional paramater $limit determins tha maximum number of results to return. If it is not given then it will return the number set in the plugin options  panel. If limit is given as 0 (zero) the ALL results will be returned.

If you set option paramater $echo as flase then instead of print the results  they will be returned so you can perform additional formatting or concatonate  them with other data. 

array referer_fetchUrls([integer limit])
array referer_fetchHosts([integer limit])
array referer_fetchWords([integer limit])
array referer_fetchPhrases([integer limit]) 

These are wrappers for function in the Referer class. Use them in you  template to get an unformatted array of results. 

They query the database for stats about top referers. They return an array of  urls, hosts, search words and searchphrases respectively. If there was a  problem or if there are no results they return NULL. 

Optional paramater $limit determins tha maximum number of results to return. If it is not given then it will return the number set in the plugin options panel. If limit is given as 0 (zero) the ALL results will be returned.



  1. April 16th, 2006 at 15:53 | #1

    hum… on my wordpress i can no more acess control panel of referer by Manage.. what could be?

    Hamish says: This problem has been resolved and was not a software bug.

  2. April 22nd, 2006 at 22:57 | #2

    I’m playing with this on my site now. I made the search keywords to refer to my own site in google, technorati, and yahoo search string and it’s working like a charm. thanks.

    Hamish says: I aim to please. :)

  3. May 10th, 2006 at 05:57 | #3

    Is there a way to use this on a protected page (one made under Write/Write Page)? I’d prefer to have the stats only for my eye.

    Hamish Says: Instructions for this available on request.

  4. May 25th, 2006 at 12:28 | #4

    Hello, this is a great plugin! Thanks for releasing it. Secondly, is there a way to pull and display the title-names of the referers and have the URLs themselves appear as quicktips?

     Hamish says: This is not something that can be achieved easily. It is possible but there a number of issues with it’s practicality. I’m considering it for future versions.

  5. May 30th, 2006 at 17:43 | #5

    I activated this plugin, and it wiped out my blog and the admin. It displays blank now. Any ideas how to fix?

    Hamish says: Any serious errors caused by the plugin can be resolved by deleting the referer.php file from your wp-content/plugins directory. I suspect this problem has something to do with a conflict with the podPress plugin.

  6. June 3rd, 2006 at 09:06 | #6

    Hi there Nice plugin. I’d like to request a feature. Settings in admin should make it possible to display only the selected options in the result. Meaning; I’d like only to show ULS or Phrases or Hosts or … On my site, I’d like to show only display only "Phrases". How would I do that. Entering "0" (zero) in "Top Referers Limits", displays everything. What could I use now to display only the phrases ? Rgds Jakob http://www.boyer-draeby.dk/

    Hamish Says: To cut a long story very short use referer_topPhrases() in your template.

  7. June 3rd, 2006 at 18:47 | #7

    Man, sorry, couln’t see the forrest for all the trees :) Thx Jakob http://www.maxthon.dk (Demo)

  8. June 3rd, 2006 at 18:49 | #8

    I’d like however still to request nother feature (hope that I didn’t miss anything this time): The ability to display the "stats"  on individual pages, so that the stats are shown on the right pages. Meaning…..: On a specifc page, only the referers to that page are shown, etc . Possible ? Rgds Jakob at http://www.maxthon.dk

  9. June 3rd, 2006 at 21:04 | #9

    Hmmm, I see now that you plugin can not handle danish characters like Æ, Ø, Å (the three extra danish characters)… Is there a way to change that ? Rgds Jakob

    Hamish says: Open the main referer.php plugin file. On line 831 replace:
        $part = preg_replace(‘/[^a-z0-9"]/i’, ”, $q);
    with:
        $part = str_replace(array(‘+’, ‘-’), ”, $q);

  10. June 11th, 2006 at 01:41 | #10

    Great plugin, but i\’d like to suggest that there be an option to specify the server path to the logfiles.like just a textbox that you could enter /home/user/site/logs/access-log or whatever the path to the logs on your server so there would be no need to download them just to upload again.thanks for the plugin though, it was exactly what i was lookin for.

    Hamish Says: Feature added as of version 0.30.

  11. June 13th, 2006 at 10:56 | #11

    Hi! First of all GREAT plugin! I am trying to get it to work with swedish characters "encoding ISO-8859-1" (å, ä, ö)I tried the "Danish way" but then my site doens´t even load…. Any ideas on how to solve it?Thanks / Marcus

  12. June 13th, 2006 at 22:44 | #12

    Hi HamishIs it possible to make the plugin register what pages referers are lead to ? I\\\’d like to show page specific referers. Meaning, on “page x”, I\\\’d like to show what referers lead to “page x”. Can this be done ?RegardsJakobhttp://www.isabells.dk

    Hamish says: Unfortunately this is not currently possible. To accomplish there would need to be a major change in the database structure and so it is unlikely to be implemented soon.

  13. June 14th, 2006 at 07:39 | #13

    It would be great if there was an option to only show referers within the last 24 hours, like on the bottom of this page. This seems to have been a feature of WPMU 1.5, which is now gone.

    Hamish says: Good idea, I will definately consider it for the next version.

  14. August 18th, 2006 at 07:53 | #14

    i wanna know the coding of how to show the last visited page from which the current page is linked.

    Hamish Says: Unfortunately this is not possible with the current implementation of the plug-in. The log data does not contain any information about which page is being referred too.

  15. Fergus
    August 24th, 2006 at 14:45 | #15

    Actually I am in Chichester not Rochdale :)

  16. August 30th, 2006 at 23:23 | #16

    I made sidebar widget for this plugin :) It can be customized for title, referer shows up, output lines.

  17. Mosey
    September 2nd, 2006 at 23:29 | #17

    Hi, thanks for this plugin :) I really like it! However, I\’ve found that everytime I save/update the options – it adds an extra after certain class definitions etc. – How can I prevent this? I\’m sure it doesn\’t make a huge difference except that for instance the class factor becomes something like: I\’m using Firefox 1.5.0.6. Thank you :)

    Hamish Says: Open ref_funcs.php and go to line 315, it should look like this:

           // god damn magic quotes >:(
           if (get_magic_quotes_gpc() == 1)
               $var = $method[$key];
           else
               $var = addslashes(  $method[$key] );

    Now comment out 3 of the lines leaving the middle one so it looks like this:

           // god damn magic quotes >:(
           //if (get_magic_quotes_gpc() == 1)
               $var = $method[$key];
           //else
           //    $var = addslashes(  $method[$key] );

    Save the file and upload to your server, overwriting the old one. Now everything should work as expected.

  18. Mosey
    September 3rd, 2006 at 20:41 | #18

    Thank you very much! :D This issue has now been resolved. Now I\’m trying to decide whether its good for my host to have magic_quotes turned on!:)

    Hamish says: Well I tried emailing you twice but it gets bounced (You can find my email on the “About Me” page.) Magic Quotes are bad, bad, bad. Their only use is to baby-sit incompetent developers who would try to insert a REQUEST straight into a database, they cause endless problems, and they are getting removed in PHP6. So yeh you probably want them turned off.

  19. Mosey
    September 4th, 2006 at 19:52 | #19

    OMG… I can’t believe I was so silly! – in my haste to leave the messages I didn’t check my email address after typing it in the first time (and the cookies remembered my details for the second reply) – I have corrected it now and as you can see – the numbers are but right next to each other >_<  Thank you very much for trying though! I do appreciate it and will contact my host to find out more about the settings. :)   

  20. September 6th, 2006 at 18:58 | #20

    hamish 0.41 made my int. settings f… up. national characters are messed up. the change mentioned up page, doesn’t apply anymore .. ? rgds jakob http://www.boyer-draeby.dk

    Hamish says: Not entirely sure what is going wrong here (and that not for lack of trying) but it can be fixed by Clear Logged Data followed by an Apache Log Import. Not ideal I know.

  21. September 9th, 2006 at 23:31 | #21

    Seems to be a good idea, but, all the words and phrases are NULL?

    Hamish says: *sigh*

  22. September 10th, 2006 at 00:59 | #22

    Suggestions.

    I had to turn it off, the database will be grow way to fast.

    1.) Need the Phrases and Words in the database instead of NULL.
    2.) Need to have option for only the “visible” links inserted into the database. Otherwise the database will grow to large as it collects internal links.

  23. October 1st, 2006 at 20:59 | #23

    Database grows :) And I think it slows the site down ?

    Cheers !
    http://www.boyer-draeby.dk

  24. November 15th, 2006 at 20:40 | #24

    hallo Hamish! i too would love to get this plug in “viewable by admin only” format!
    possible to obtain a certain documentation stuff?
    thanx a ton in advance!

  25. November 23rd, 2006 at 15:49 | #25

    Hi. Got a problem with your plugin. A very little one, but truly annoying for me. Your plugin’s output, the Search Phrases section, ain’t XHTML valid. You should encode the ampersands (all the “&” in the strings got from the search engines queries should be transformed when being outputted as “&”)

  26. November 23rd, 2006 at 16:07 | #26

    Er, sorry, it’s not in the Search Phrases, but in the Top Urls section. For example:
    http://www.google.it/search?hl=it&q=uccidere+la+democrazia&meta=
    should be encoded in
    http://www.google.it/search?hl=it&q=uccidere+la+democrazia&meta=

  27. December 13th, 2006 at 20:57 | #27

    Ho ho ho! It says I live in Oxford (still not anywhere as glamorous…)

  28. deko
    January 10th, 2007 at 15:35 | #28

    congratulations for this plugin!
    it is really useful..

    but I wonder why your “Top 5 Urls” are adult sites… !?!?

    I could not find any link to your blog on those sties :(

    thanks

  29. January 22nd, 2007 at 22:51 | #29

    Can you develope a simple phpbb mod that do the same of it?
    If you can help, contact me please.

  30. September 11th, 2007 at 07:42 | #30

    i just installed this plugin on my blog and i love it! thank you!

  1. March 10th, 2007 at 11:17 | #1
  2. March 30th, 2007 at 18:46 | #2