Tracking entry referrers with PHP in MT

In: Blogging| Web development

16 Aug 2003

I’ve always wanted to track referrers for each entry and at the same time be able to tell which entries are the more popular ones by keeping track of each entry’s hit count.

So I did the smart thing first by Googling to see if anyone else had done it. I found the AWStatsReferers MT plugin at the MT Plugin Directory but it requires your host to have AWStats installed. My host doesn’t, and I wasn’t going to get them to install AWStats just so I can track referrers for my blog. Refer is a referrer tracking script that turned up – it actually seems like it could do what I wanted with a little tweaking, but a look at the code left me thinking there should be a simpler way to achieve my more specific goals (i.e. track referers and, consequently, hit count for each entry).

So, hey, I decided to whip up a simple hack of sorts using PHP and a database table to store the referrers.


Tracking entry referrers with PHP in MT

Requirements: MovableType, webhost that supports PHP, database (MySQL in this tutorial)

A plain text version of this tutorial is also available here.

  1. First off, any page where you wish to display the referrers or hit count, including your archives and main index page, need to have a .php extension (or any other extension that will cause the file to be pre-processed by PHP). You can do so by renaming your Archive File Templates to have a .php extension (go to Weblog Config -> Archiving). The same goes for your main index page (go to Templates) .
  2. Create the database table. The SQL is:

    CREATE TABLE `mt_entryhits` (
    `entry_id` INT(11) NOT NULL
    REFERENCES mt_entry(entry_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
    `referer` VARCHAR(255) NOT NULL
    );

    You can use either phpMyAdmin, the MySQL command-line client or whatever suits your boat.

  3. Create a file named db-config.php with the following code:

    <?php
    $host = ‘host’; // database server hostname
    $user = ‘user’; // database username
    $password = ‘password’; // database password
    $database = ‘database’; // MT database
    ?>

    Replace the values of the variables as per your configuration. Upload it in your blog’s top level directory (i.e. the Local Site Path of your weblog, as specified in the Weblog Configuration screen).

  4. Login to your MovableType installation and Manage your weblog. Go to Manage -> Templates -> Archive-Related Templates -> Individual Entry Archive.
  5. At the top of the template, add the following lines:

    <?php
    error_reporting(0);

    include( ‘<$MTBlogSitePath$>db-config.php’ );

    mysql_connect( $host, $user, $password );
    mysql_select_db( $database );

    // this is the list of referrers you don’t want to record
    // use the following format within the parentheses:
    // ‘http://domain1.com’, ‘http://domain2.com’, …
    // leave it blank if you want to record all referrers
    $ignore_list = array( ‘http://referrer.you.want.to.ignore’ );

    // check referrer string to determine if we want to record it
    $ignore_referrer = false;
    foreach( $ignore_list as $ignore ) {
    if( stristr( $_SERVER['HTTP_REFERER'], $ignore ) !== false ) {
    $ignore_referrer = true;
    break;
    }
    }

    // record the referer
    if( !$ignore_referrer ) {
    mysql_query( “INSERT INTO mt_entryhits(entry_id, referer) VALUES (<$MTEntryID$>, ‘{$_SERVER['HTTP_REFERER']}’)” );
    }

    // get the no. of times this entry has been read
    $rs = mysql_query( ‘SELECT COUNT(*) FROM mt_entryhits WHERE entry_id=<$MTEntryID$>’ );
    $row = mysql_fetch_row( $rs );
    $hits = $row[0];

    // get the list of referers
    $sql =
    ‘SELECT referer, COUNT(*) AS count
    FROM mt_entryhits
    WHERE entry_id=<$MTEntryID$>
    GROUP BY referer
    ORDER BY count DESC’;

    $rs = mysql_query( $sql );
    ?>

    This code snippet is specific to MySQL – you should be able to change it easily for PostgreSQL or SQLite (if not, let me know and I’ll post code snippets for those RDBMSs too). Of course, you can plug in a database abstraction library like the excellent ADODB, but I leave out that portable solution in favor of database-specific built-in function calls because MovableType supports only MySQL, PostgreSQL, or SQLite.

  6. In the same template (the Individual Entry Archive template), look for a good place to display the entry hit count and insert the following code:

    Read <?=$hits?> time<?php if($hits != 1) echo ’s’;?>

    I place them in the “Posted by” bit at the end of each entry, which for the default MT template is

    <span class=”posted”>Posted by <$MTEntryAuthor$> at <$MTEntryDate$>
    <MTEntryIfAllowPings>
    | <a href=”<$MTCGIPath$><$MTTrackbackScript$>?__mode=view&entry_id=<$MTEntryID$>” onclick=”OpenTrackback(this.href); return false”>TrackBack</a>
    </MTEntryIfAllowPings>
    | Read <?=$hits?> time<?php if($hits != 1) echo ’s’;?>
    <br /></span>
  7. Now, it’s time to display the referrers. Again in the same template, add the following code where you wish to display the referrers.

    <?php
    // display the list of referers
    echo ‘<div class=”sidetitle”>Referrers</div>’, “\n”;
    echo ‘<div class=”side”>’, “\n”;
    echo ‘<ul>’, “\n”;
    while( $row = mysql_fetch_assoc( $rs ) ) {

    $referer = $row['referer'];
    $count = $row['count'];

    echo ‘<li>’;

    if( strpos( $referer, ‘http://’ ) !== false ) {
    echo ‘<a href=”‘, $referer, ‘” target=”_blank”>’;

    // truncate URL if too long
    if( strlen($referer) > 90 ) {
    $referer = substr($referer, 0, 90) . ‘…’;
    }
    echo $referer, ‘(’, $count, ‘)’;
    echo ‘</a>’;
    }
    else {
    // truncate URL if too long
    if( strlen($referer) > 90 ) {
    $referer = substr($referer, 0, 90) . ‘…’;
    }

    // handle cases where there is no referrer
    if( $referer == ” ) {
    $referer = ‘Direct hit or unknown referrer’;
    }
    echo $referer, ‘(’, $count, ‘)’;
    }
    echo ‘</li>’;
    echo “\n”;
    }
    echo “\n”;
    echo ‘</ul>’, “\n”;
    echo ‘</div>’;
    ?>

    You can format the HTML to suit your current template.

  8. Phew! The hardest part is now over. All that’s left is to display the number of times each entry has been read in the other archive pages and the main page (Main Index). I’ll only work through the changes to the Main Index template, since it will be similar for the other templates (Category Archive, Date-Based Archive, any other templates you have created that displays blog entries).

    At the top of the template, add the following lines:

    <?php
    error_reporting(0);

    include( ‘<$MTBlogSitePath$>db-config.php’ );

    mysql_connect( $host, $user, $password );
    mysql_select_db( $database );
    ?>

    Look for the <MTEntries> tag in the template and add the following lines after it:

    <?php
    // get the no. of times this entry has been read
    $rs = mysql_query( ‘SELECT COUNT(*) FROM mt_entryhits WHERE entry_id=<$MTEntryID$>’ );
    $row = mysql_fetch_row( $rs );
    $hits = $row[0];
    ?>

    Now that we’ve the number of hits in the variable $hits, we can display the entry hit count as before by adding the following line of code within the <MTEntries> tag:

    Read <?=$hits?> time<?php if($hits != 1) echo ’s’;?>
  9. Do the same for the other archive templates (Category Archive, Date-Based Archive, any other templates you have created that displays blog entries).
  10. Rebuild your site and you’re done!

Thanks to everyone who gave comments and asked questions at the MovableType support forums and elsewhere, who pointed out things I overlooked.

Update: Matt Moore has an alternative (based on this very post! ;-)) that includes collection of the page title of the referrer, and also the search terms used to get to your blog entry. Check out his entry entitled “How to collect referrer data

81 Responses to Tracking entry referrers with PHP in MT

Avatar

Segmentation Fault: Core dumped..;-)

July 21st, 2004 at 2am

I am off..
..for a few days, that is. Will be taking some time off from active blogging starting this very moment and will be back in business by 07/20/2004 08/01/2004, if not sooner. Have a few personal, professional and some blog related housekeeping to do. Th…

Avatar

Fembat.Net :: Journal

November 24th, 2003 at 9pm

MT Individual Entry Referrals
I’ve always wanted a more precise method of tracking referrers for each entry, and finally after months of trying to come up with something myself I finally fell upon a terrific MT Hack at redemption in a blog. This also…

Avatar

pixelgraphix

March 7th, 2004 at 2am

Ressourcen für Movable Type
Wer auf der Suche nach einem Publishing-System für die eigene Internet-Präsenz ist, stößt früher oder später auf Movable Type. Ein Programm, das von Six Apart vor allem als Weblog-Werkzeug entwickelt und unterstützt …

Avatar

pixelgraphix

March 23rd, 2004 at 2am

Und sie validieren doch
Auf pixelgraphix verwende ich die Referrer und Hits Zählung, die von Cheah Chu Yeow auf redemption in a blog veröffentlicht wurde. Durch die Anzeige der Referrer und der in ihnen enthaltenen Sonderzeichen fielen die entsprechenden Seiten beim…

Avatar

pixelgraphix

March 23rd, 2004 at 2am

Und sie validieren doch
Auf pixelgraphix verwende ich die Referrer und Hits Zählung, die von Cheah Chu Yeow auf redemption in a blog veröffentlicht wurde. Durch die Anzeige der Referrer und der in ihnen enthaltenen Sonderzeichen fielen die entsprechenden Seiten beim…

Avatar

pixelgraphix

March 23rd, 2004 at 3am

Und sie validieren doch
Auf pixelgraphix verwende ich die Referrer und Hits Zählung, die von Cheah Chu Yeow auf redemption in a blog veröffentlicht wurde. Durch die Anzeige der Referrer und der in ihnen enthaltenen Sonderzeichen fielen die entsprechenden Seiten beim…

Avatar

pixelgraphix

March 23rd, 2004 at 9pm

Und sie validieren doch
Auf pixelgraphix verwende ich die Referrer und Hits Zählung, die von Cheah Chu Yeow auf redemption in a blog veröffentlicht wurde. Durch die Anzeige der Referrer und der in ihnen enthaltenen Sonderzeichen fielen die entsprechenden Seiten beim…

Avatar

shirtrat(dot)net

March 24th, 2004 at 7am

A few site changes
Long time no post. It’s been almost two weeks. Haven’t had much to say actually. Been working behind the scenes on a few things with my blog. I added a referrer to the bottom of each individual entry so you…

Avatar

hello-lovely

July 6th, 2004 at 11pm

More Blogsnobbery
Because I have nothing of importance to post (most of you probably already know that Kerry picked John Edwards as his running-mate), here’s another journey through Blogsnob Land, ended by a page without a blogsnob link! :( + Fembat blogs…

Avatar

highlyoverrated.info - do you need more?

November 9th, 2003 at 7am

mt resources, die x-te
für mt gibt es alles und das auch noch tonnenweise ;) und das ist genial so: redemption in a blog…

Avatar

meowy's Got Stuff to Say

October 20th, 2003 at 6am

Counts! Project for tomorrow
Yay! Thanks to Violet at the MT forums, I think I’ve got just what I need. :) Her site is…

Avatar

distant, early morning

October 26th, 2003 at 6pm

Entry hit counter and referrers
I was asking about some stuff over on the movable type support forums, which a wonderful gal named Violet answered. Because she was so helpful I was curious and popped over to her weblog. I noticed that she had hit…

Avatar

//gtmcknight

October 6th, 2003 at 5am

Random Updates…
First, and foremost Radiohead was one of the best concerts I’ve ever been to. They are simply amazing, and the outdoor amphitheatre made it alot of fun as well. Thom Yorke’s performance was moving, funny, exciting, and hypnotic all at…

Avatar

The Blog of the Century of the Week

September 21st, 2003 at 8pm

PHP and MySQL: How to collect referrer data
Ok, here’s a special interest post: How to collect and display data on referrers, search terms, and number of reads on an individual post basis. You’ll need PHP, a MySQL database, and enough patience to work with your templates a…

Avatar

The Blog of the Century of the Week

September 14th, 2003 at 3am

Changing some scripts
I’m junking a bunch of CGI scripts and going to PHP. I’m doing this because it’s much easier to put PHP data in a page, since you just set your variables at the top and then you can call them…

Avatar

JayAllen - The Daily Journey

September 6th, 2003 at 5pm

Tracking entry referrers in MT

Avatar

Cookie Crumbs

August 31st, 2003 at 2am

Tracking entry referrers with PHP in MT
Tracking entry referrers with PHP in MT Want to know who is coming to see your entries? Track them!…

Avatar

Geeky Bookmarks

September 6th, 2003 at 9am

Tracking entry referrers with PHP in MT
redemption in a blog: Tracking entry referrers with PHP in MT via Christine’s Cookie Crumbs…

Avatar

scriptygoddess.com

August 22nd, 2003 at 1pm

Referrer script and entry hit tracker
Donna posted a few neat little tidbitds – but the one I was most interested in was the referrer script that also tracks hits on an entry… Like “read 100 times” etc. Here’s the script from redemption in a blog….

Avatar

kiesows.de

August 27th, 2003 at 12am

referrers
meiner einer hatte mal wieder lust etwas am blog zu verändern, und da ich neulich über Tracking entry referrers with…

Avatar

Pinging Knight

August 27th, 2003 at 10pm

Minor Blog Update
Made a few minor updates to the design and workings of my blog: Comments are now numbered. Your email will…

Avatar

Live in the Delirious Cool

August 22nd, 2003 at 2am

Posted elswhere, counted here.
Two more excellent advanced widgets for tracking what’s said and seen here and elswhere – and a plug for my favorite webring.

Avatar

Considering...

August 19th, 2003 at 3am

A better wheel. Maybe.
Today, I tried to install a bit of support for tracking entry referrers. As you can see, I haven’t had much luck getting it to work. I’ll probably undo that work in a little while unless I find out how…

Avatar

unchained thoughts

August 19th, 2003 at 4pm

focus
Thank you for everyone’s thoughtful and helpful suggestions about my wedding woes. I am feeling better about it now, trying to think about what we want and I will reconcile the whole dress thing in the months to come. Much…

Avatar

lovelinks

August 17th, 2003 at 8pm

entry referrers
redemption in a blog: Tracking entry referrers with PHP in MT – uses MySQL database, and looks much more elegant…

Avatar

kadyellebee

August 18th, 2003 at 6am

linkyness
I’m finding such good links while catching up on news reads that I just had to share some of them….

Avatar

Links

August 19th, 2003 at 12am

http://ko.offroadpakistan.com/links/cat_mt.html#000481
Tracking entry referrers with PHP in MT…

Avatar

Marina

August 17th, 2004 at 11pm

I wonder if I have a normal blog site structure..I would appreciate your coming to my blog and leaving some “fresh” ideas and proposals concerning what it is better to rebuild or reorganize.
I really need your help …. Thanks.

Avatar

Marina

August 17th, 2004 at 11pm

I wonder if I have a normal blog site structure..I would appreciate your coming to my blog and leaving some “fresh” ideas and proposals concerning what it is better to rebuild or reorganize.
I really need your help …. Thanks.

Avatar

Marina

August 17th, 2004 at 11pm

I wonder if I have a normal blog site structure..I would appreciate your coming to my blog and leaving some “fresh” ideas and proposals concerning what it is better to rebuild or reorganize.
I really need your help …. Thanks.

Avatar

Chris

July 25th, 2004 at 8pm

I managed it to get your plug-in working. Now I would like to know how to change the code to display just the last 20 referrers (like on this page), not all.

Further I would like to know how and where to put the code to change the referring link, so that spammers cannot earn pagerank (I am using an outbound.pl cgi script and need to know where to put the ‘http://www.digitalvoodoo.de/cgi-bin/outbound.pl’)

Please answer directly here in your blog as it seems that I cannot receive your e-mails :-(

Kind regards,

Chris

Avatar

Cheah Chu Yeow

July 25th, 2004 at 10pm

Hi Chris,

>> to display just the last 20 referrers

Add the first 2 lines below and the last line at the end of the while-loop:

$displayMax = 20; // max. no. of referrals to display
$displayCounter = 0;
while( $row = mysql_fetch_assoc( $rs ) ) {
$referer = $row['referer'];
$count = $row['count'];

// … other stuff

echo “\n”;

if( ++$displayCounter >= $displayMax ) break;
}

>> where to put the code to change the referring link

Instead of:

echo ‘<a href=”‘, $referer, ‘”>’;</a>

Use:

echo ‘<a href=”http://www.digitalvoodoo.de/cgi-bin/outbound.pl?url=’, $referer, ‘”>’;</a>

Customize the URL as per your Perl script.

Avatar

irish

August 7th, 2004 at 4am

I need to know, can this script report a hits?
E.g : when somebody search some keyword in a search engine, and your site with that script is displayed in that current search (but not clicked/visited) . Can it report that ‘thissite.php’ is hit when somebody do a : http://www.google.com/?query=search+free+ebook&display…bla..bla.

If it can do like that .. it will be easy to track what’s best keyword for our site .
thanks.

ps: I also made a tracking referer with a plain text.
check it out at http://newbiewebmaster.blogspot.com

Avatar

Mariam

June 22nd, 2004 at 1pm

I used this for my article database /articles and I was hoping if you could tell me how perhaps do.

“The [number of articles] have been read a total of [number] times.”

There are so many posts above and are way over my head. I’m sorry if this was asked/answered above.

Avatar

Chris

July 6th, 2004 at 5am

Sorry, but I haven’t received an email from you yet. :-(

chris@digitalvoodoo.de

Avatar

Cheah Chu Yeow

May 30th, 2004 at 2pm

Hi Chris,

It seems that something was mistyped or mis-copied, because the PHP is being parsed as plain text. If you could show me the template code you have beginning at the referrers part, I would be able to debug it for you :)

PS. I’ve been trying to get you via email but it doesn’t seem to be working.

Avatar

Chris

June 7th, 2004 at 1am

Thank you for you answer, unfortunatelly my spamfilter is not only fighting spammails :-(

Here is my Template code beginning with the referrers part in the individual entry archive template (at the end of the page):

Die letzten 10 Referrer

Note to referrer spammers: The referral links below will have no effect on your Google PageRank. Besides, once you’re in my referral logs, you will be blocked. Save yourself the trouble.

// display the list of referers
echo ‘

Referrers

‘, “\n”;
echo ‘

‘, “\n”;
echo ‘

‘, “\n”;
echo ‘

‘;
?>

You can also answer me by e-mail, I will check my spam folders ;-)

Cheers,

Chris

Avatar

Chris

May 30th, 2004 at 9am

Anybody out there who might help me?

Avatar

Chris

May 22nd, 2004 at 10am

Hi, thank you very much for this tutorial, it is really great. Unfortunately it isn’t working 100% on my site, like you can see here at the bottom of the page: http://www.digitalvoodoo.de/singapur_hongkong/archives/vorbereitungen/lonely_planet_south_east_asia_thailand_malaysia.php

The Hit-count for each entry is working but the referrer list won’t work. Any idea what might be the problem?

It would be great if you get back to me :-)

Cheers,

Chris

Avatar

Cheah Chu Yeow

April 6th, 2004 at 8pm

can you also use this script to show the top hits to entry?

Theoretically, you could, but I can’t figure out how to figure out the permalink to an entry with just the entry ID.

Avatar

iced glare

April 6th, 2004 at 10am

can you also use this script to show the top hits to entry? Like in the sidebar?

Avatar

brad

November 20th, 2003 at 3pm

the code didn’t show up…

on line 26 i changed <$MTEntryID$> to 0 (zero)

also, you’ll have to “view source” or “save as” in order to get the code.

Avatar

Karen

December 28th, 2003 at 3pm

I have been wanting to implement this for a while now, but I have no idea how to do any MySQL-related stuff. I’m also on a Mac and have no idea what command line is.

Is there an easier way to do this?

Avatar

Gregz

January 7th, 2004 at 7pm

I’ve got me a little catch 22 problem here. For obvious reasons I want to ignore my own site as referer, but when I put it in the ignore array it stops counting as well. Would it not be more elegant to ignore them when displaying?

Avatar

Carin

January 30th, 2004 at 6pm

THis is exactly what I neded

Avatar

carlo

February 16th, 2004 at 8pm

lovely css page, very impressive. wow :)

Avatar

Manuela

March 20th, 2004 at 11pm

I absolutely love this solution and it works great. However, there is a validation problem. Some referrer URLs display special characters that need encoding. I have tried the textile plugin but did not get it to work on this issue. Maybe someone could help?

Avatar

brad

November 20th, 2003 at 3pm

if you want a quick and dirty way of seeing the stats without running up counts. stick “this php script”:http://www.thatbradguy.com/downloads/log.txt in your blog root (the same place you put the db-config.php file). change the filename to .php.

it will list your entries (with title) that have been read in the order of most popular to least popular with the number of times they have been read beside them. then below it will display each entries referers.

note: in order to keep up with the stats on my home page i changed the php code in step 5. on my homepage *only* i changed line 26. i replaced with 0 (zero). that way when it lists them in this log it has an “entry_id” for the home page. you could do this with any other page on your site as well, so long as you keep the numbers unique and make sure you don’t have a blog entry with the same number.

if you do this to any other page you’ll have to hack my script on lines 26-32 to reflect what you’ve done. the “entry_id” of 0 is already setup to display “Homepage” as its title.

Avatar

Cheah Chu Yeow

November 14th, 2003 at 12am

Yup it does, unless you configure your server to treat any other extensions
to be parsed as PHP files. Then, you can use that extension.

Avatar

craig1972

November 13th, 2003 at 2pm

Hi, does this plugin require my pages to have a PHP extension?

Avatar

Cheah Chu Yeow

November 7th, 2003 at 12am

I’ve updated the instructions to allow ignoring of specific referrers from being tracked. Check out the code comments in point 5 for more details.

It shouldn’t break any existing installations, but in the event that it does, do let me know.

Avatar

Cheah Chu Yeow

October 27th, 2003 at 5am

Hi lisa, you may want to take a look at Matt Moore’s How to collect referrer data blog entry.

I’ve been too lazy to include instructions on how to exclude specified referrers, but if you want, I will do so.

Avatar

lisa

October 26th, 2003 at 9pm

Hey,

I was wondering – is it possible to make this ignore all hits from a certain ip? I’d like to be able to check referrers etc without running up hit counts…

-Lisa

Avatar

Cheah Chu Yeow

September 6th, 2003 at 7am

Les: I realise now my mistake and have updated the entry. You shoulld keep it as <$MTBlogSitePath$> but upload db-config.php to your blog’s local site path instead (same directory as your blog’s index page).

aman: If you have phpMyAdmin, you can run create the database if you cut-n-paste the SQL and run the SQL. Otherwise, if you can get shell access (telnet or SSH), run _mysql_ and input the SQL.

Avatar

aman

September 5th, 2003 at 9pm

in pace 2 , you said: “Create the database table…you can use either phpMyAdmin, the MySQL command-line client or whatever suits your boat”.

But,my Q is: how can i use that codes in Mysql, in the other hand, how can i creat the database table in mysql?

Avatar

Les

September 2nd, 2003 at 10pm

Of course, now I’m getting a completely different error:

Warning: main(): stream does not support seeking in /home/les/public_html/seb/archives/2003/09/02/ toyotas_newest_prius_parks_itself.php on line 3

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/les/public_html/seb/archives/2003/09/02/ toyotas_newest_prius_parks_itself.php on line 13

Avatar

Les

September 2nd, 2003 at 10pm

I was having trouble getting this to work as well as it wasn’t putting any data into my database either. Looking at the code more closely I discovered the reason why. I host more than one weblog at my site and all of them have different domain names than where I store my MT script. As a result the use of $MTBlogSitePath$ in the Include results in the script not finding my db-config.php file. Instead, you should modify the script to use $MTCGIPath$ in all the Include statements so it knows the proper place the find the file.

Avatar

Cheah Chu Yeow

August 30th, 2003 at 5pm

Hi Sasha. Glad you find it useful.

I think it actually works for more than one blog in the same database, since MT keeps the entry IDs of blogs unique across all blogs in a MT setup.

Just do the same (meaning changing the templates as I’ve written) for all other blogs and it should work fine. You can skip the steps of creating the database table (mt_entryhits).

Avatar

Sasha

August 30th, 2003 at 5pm

I really love this script, it works like a charm!
I just noticed, you can’t use this for more than one blog in the same database, right? I mean, I’d have to edit the table names and stuff. Maybe a suggestion for the next version to build this one? It would not be very hard, I imagine, to add the blogID to each refer as well? :) Great job on this though!!

Avatar

Peter Bowyer

August 24th, 2003 at 5pm

Nice work. What would be nice is a MT plugin to do the above – then it will work without the need for PHP to interpret the pages. Meaning you could expand this to work with all pages on the site, not just articles.

Pluse, using the built-in database connection the code will be the same for all supported databases.

Avatar

Cheah Chu Yeow

August 19th, 2003 at 5pm

Thanks to “Sam”:http://www.lollygagger.org/stb/plog and to “Jane”:http://www.unchainedthoughts.com/, who both managed to get it working for communicating with me their experiences.

There seems to be no problems with the code (though the instructions probably need to be worked over to be easier to understand).

I’ve a few suggestions for those of you (including Ryan) who couldn’t yet get it to work.

* make sure your files are .php files and you’ve rebuilt
* make sure you’ve the host, user, password and database specified correctly in db-config.php

Avatar

Cheah Chu Yeow

August 19th, 2003 at 1pm

Christian: I’ve edited it. Thanks again.

Avatar

Ryan

August 19th, 2003 at 3pm

Thanks for the text link. I was going to try that before waiting to see what happens. There were a few missing “php echo” bits, but nothing, it seems, that changed the fact that referrals aren’t being recorded in mt_entryhits. I’ll wait a bit longer.

Avatar

Christian Sobetzko

August 19th, 2003 at 1pm

redemption: No problem. Could you edit my comment from August 19, 2003 11:14 AM because <$MTEntryID$> has been removed and the query will not work.

This is the line:

$rs = mysql_query( 'SELECT referer, COUNT(*) FROM mt_entryhits WHERE entry_id= GROUP BY referer' );

entry_id is missing the MTEntryID thing.

Avatar

Cheah Chu Yeow

August 19th, 2003 at 12pm

Ryan: Oh yes one more thing – if you can’t see the code very well in Safari, there is a text version “here”:http://blog.codefront.net/archives/2003/08/16/tracking_entry_referrers_with_php_in_mt.txt. (link is also available above). At least until I find a good way to display long lines of code without overflowing.

Avatar

Cheah Chu Yeow

August 19th, 2003 at 12pm

KO: following Christian’s suggestion should allow you to limit the no. of referrers stored.

Ryan: I’ve had someone with the same problem too. I’ll look into her’s and get back to you – maybe I forgot something. Sorry about the collapsed divs – I’ve been trying to figure out a good way of displaying the code without the text overflowing into out of the div. Apparently there is still some more work to be done.

Christian: Thanks for your suggestion on using GROUP BY! I’ve implemented your suggestion in the tutorial. And thanks for being my ‘tech support’ ;).

Avatar

Christian

August 19th, 2003 at 11am

Hm, the empty box wasn’t enty when I preview. Anyways, here it is again.

Replacement of step 5 “At the top of the template, add the following lines:”

 

Avatar

Christian

August 19th, 2003 at 11am

If you want to limit the number of referers stored in the db this should work (untested)

redemption: maybe a small suggestion: Instead of counting inside the while loop try using count() and group by.

$rs = mysql_query( 'SELECT referer, COUNT(*)
FROM mt_entryhits WHERE entry_id=<$MTEntryID$>
GROUP BY referer' );

Avatar

Ryan

August 19th, 2003 at 3am

I am also using the Refer script. Perhaps the referrals are being eaten by that script. Just a wild guess.

Avatar

maraboutslim

August 18th, 2003 at 11pm

never mind! i think i’ve found it. configuration/preferences. thanks.

Avatar

KO

August 19th, 2003 at 12am

I am already using the Refer script. Can I use both at the same time, or should I delete the refer script first before?

Also, can the number of referrers saved in the db be limited?

Avatar

Ryan

August 18th, 2003 at 6pm

I’ve followed the directions, but nothing is happening, database-wise. Perhaps I didn’t get all of the code? Safari is making those text areas too small (especially for the one-liners.)

Avatar

maraboutslim

August 18th, 2003 at 10pm

hi, can you expand on this part. i’m not getting it. What do I enter to rename the files .php?

“First off, any page where you wish to display the referrers or hit count, including your archives and main index page, need to have a .php extension (or any other extension that will cause the file to be pre-processed by PHP). You can do so by renaming your Archive File Templates to have a .php extension (go to Weblog Config -> Archiving). The same goes for your main index page (go to Templates) . ”

thanks,

slim

Avatar

sam

August 18th, 2003 at 4pm

aha!
thanks so much!
works great now
great job!
-sam

Avatar

Cheah Chu Yeow

August 18th, 2003 at 4pm

Sorry I wasn’t a bit more clear. That’s your database host name (check with your webhosting service provider if uncertain). In most cases it will be ‘localhost’, though not always.

Thanks for letting me know – I’ll go edit the entry to explain the variables a bit.

Avatar

sam

August 18th, 2003 at 4pm

hi, I’m trying to set this up but can’t seem to get it working
I think my problem may be in the db-config.php

what host is this referring to?

$host = ‘host’;

Avatar

Paul Scrivens

August 18th, 2003 at 4am

Great article. I will definitely have to implement this some time.

Avatar

SSP

September 27th, 2004 at 2pm

How long i searched for a Hack like this!
Thank you very much, but i habe a mistake somewhere and my knowledge is to small to find it.

If i add this line:

$rs = mysql_query( ‘SELECT COUNT(*) FROM mt_entryhits WHERE entry_id=< $MTEntryID$>’ );

i get the following error:

Parse error: parse error, unexpected T_STRING in /www/htdocs/denkblog/index.php on line 73

(line 73 is the Line with $rs)

I would be so glad if you could give me a hint.
Thanks!

Avatar

Hongkong

November 5th, 2004 at 7am

Thank you once more for this great tutorial. It’s workling fine on my weblog, but I have another question:

Is it possible to display a top-10 list of the most read articels on the index site? I think this would be a great gimmick.

Cheers,

Chris

Avatar

FiReaNG3L

April 6th, 2005 at 8am

I would be highly interested in a “top 10″ of the most read posts… I sadly use titles AND date (/archives/day/month/title) for my posts, and I can’t figure how to rebuild these links from my MT database (the URL isn’t stored).

Avatar

FW

March 9th, 2006 at 7am

Great tutorial, you can see the script in action on my site.