remotedotcomments

Want to use dotcomments in your blog, but your host doesn't support PHP? Phil Ulrich figured out how to host dotcomments on a separate free host that does support PHP, but now he has moved on to another comment system, then on to another host and domain name, and back to dotcomments, and his tutorial has gone missing along the way, so this is my modified version of it.

Find a PHP host

The first step is the hardest: you need to find a free host that supports PHP, isn't down most of the time, and preferably has FTP access and no ads (especially no popups, which tend to ruin the comment count). At the moment, that narrows it down to barrysworld, and sometime again, digitalrice. Sign up for an account with either one (or both, if you can, so you can switch back and forth), and once your account is activated, connect with your ftp program, create a directory named comments and chmod the directory to 777 (set Read, Write, and Execute permission for Owner, Group, and World). If you want to put comments.php in a subdirectory in your free host account, that's fine, just create the comments directory as a subdirectory of the directory where you plan to put comments.php.

Edit comments.php

Next (or while you wait for your account to be activated), download remotedotcomments.zip, my lightly modified version of dotcomments, and unzip it. Open comments.php in your favorite text editor. Edit the variables at the top as instructed in the dotcomments readme (the only one you absolutely have to edit is $comments_path).

Upload the files

Upload comments.php, commentcount.php, and dcbanner.gif to your free PHP host. Note that they do not go in the comments directory, but rather one level up: comments should be empty until it starts to fill with comment files. While you are at it, upload some sort of an index.html, so that it doesn't look like you are just abusing their generosity in supporting PHP.

Edit your template

Now, to edit your Blogger template. Somewhere between the <head> and </head> tags paste the following, changing www.mydomain.com to the address for your free PHP site:

<script type="text/javascript" src="http://www.mydomain.com/commentcount.php" defer="defer"></script>
<script type="text/javascript">
function viewComments(n) {
commentWindow=window.open('http://www.mydomain.com/comments.php?' + n, 'comments' + n, 'directories=0,height=480,location=0,resizable=1,scrollbars=1,toolbar=0,width=515');
}
function countcomments(){
 if (document.childNodes){
  if(typeof(commentID) != "undefined"){
   for (i=0;i<commentID.length;i++){
    oSpan = document.getElementById(commentID[i]);
    if(oSpan){
     oSpanText = oSpan.firstChild;
     while(oSpanText.nodeType != "3"){
      if(oSpanText.hasChildNodes)
       oSpanText = oSpanText.firstChild;
      else
       return false;
      }
     oSpanText.nodeValue = commentCount[i];
     }
    }
   }
  }
 }
</script>

In your <body> tag, include onload="countcomments()" so that you have something like:

<body link="#336699" vlink="#003366" alink="red" onload="countcomments()">

In the spot in your byline where you want the comment link to appear, include:

<a href="javascript:viewComments(<$BlogItemNumber$>)"><span id="comment<$BlogItemNumber$>">comment</span></a>

Finally, if you want to include the dotcomments banner, use:

<a href="http://www.foreword.com/dotcomments.php"><img src="http://www.mydomain.com/dcbanner.gif" width="88" height="31" border="0" alt="Comments by dotcomments"></a>

Again, replace www.mydomain.com with the address of your PHP site.

Publish and test

Publish your blog, and check it out. You should see links below each post that say "comment". Click one and make a test comment, and then reload your blog to see if the comment link changes to "1 comment" (note that comment counting only works if you are using a DOM compliant browser: IE 5+, Netscape 6, sometime in the future Opera). Once it's working, republish your archives if you want comments there as well.




January 12, 2002
After taking far too long to do it, finally fixed the fact that my "fix" for Opera lying about document.createElement actually made things worse, since it broke the comment count in IE 5.x. What should work in IE5 and not throw an error in Opera is:

function countcomments(){
if (document.childNodes){

(note that it's "childNodes" which tests for the existence of the method childNodes(), rather than hasChildNodes, which should return "true" but doesn't in IE5). If you installed remote dotcomments over the last twenty-some days and couldn't figure out why it worked for some people and not for others, well, sorry about that. I would claim that from now on I'll test everything in every popular version of every popular browser, but I've said that before, and it never seems to be true for very long. So don't be shy about telling me that my stuff doesn't work, but remember to tell me what version of what browser on what OS it isn't working on.



January 3, 2002
Testing my beta RSS comment feed for dotcomments. If you have a RSS reader, you can grab the feed for a comment thread from the XML gif down below the posting form (be bandwidth-kind: you probably don't need to update it every five minutes). While it's still very much beta (is that clear?), the source is available from rsscomments.phps (click to check it out, or right-click and "save target as" to get your copy).

Why would you want to use such a thing? Suppose over the course of a day's blog surfing, you comment in five threads on three blogs, and see another couple of comment threads where you would like to see a reply. If you are at all like me, you might remember to check one or two the next day. Or, suppose you see a post that you know a certain blogging heavyweight will be commenting on, but he hasn't got there yet. rsscomments.php will let you add a feed that doesn't have comments yet, so you don't have to keep loading the whole blog while you wait for the fireworks to erupt.

Why would you want to provide it? If just the coolness of having an RSS feed for your comments isn't enough, how about cutting down your bandwidth by not having people constantly reload your page just to see if there are new comments? It should also encourage more interaction, since people are more likely to see responses to their comments. And did I mention that it's cool?

How to use it: save a copy, open it in a text editor, change the $commentpath to the path to your comments.php file, add your email address, and upload it to the same directory as comments.php. Grab a copy of the XML gif (copy it out of one of my comment threads) and upload it there too. Find a spot in comments.php where you want to put the gif, and add <a href="http://www.mydomain.com/path/rsscomments.php?<? echo $commentID; ?>"><img src="xml.gif" alt="RSS feed" height="14" width="36"></a>

Not quite right bits: it claims to be RSS version 0.91, but will return more than 15 items (though there are an awful lot of 0.915 feeds like that); no doctype (why point to the Netscape page that will inevitable get pulled now that someone is claiming that RDF/RSS infringes on their patent?); the "no comments found" feed is a bit off, since it returns an item with the current date, which makes your feed reader think it's a new item, so it alerts you to the exciting fact that there were "no comments found!"



December 22, 2001
Updated the countcomments function to defeat Opera's latest lies. Version 6 reports "true" for "document.createElement" (and indeed, it might be true, but since Opera won't do anything with a created element, it ruins if(document.createElement) as a DOM-compliance check), so I changed the line in countcomments to if(document.hasChildNodes), which thus far keeps Opera away from things it doesn't understand.


December 9, 2001
I added a comment preview feature to my dotcomments, since I'm mostly using it in places where people will be trying to post sample code that may or may not be eaten by dotcomments. It's pretty brutally hacked into the code (someday I really need to learn how to write PHP, rather than just doing whatever works...), but if you are interested in trying it, email me and we'll work something out.


August 5, 2001
Moved my main "how to" to a permanent post at the top of the page. Use any older instructions below at your own risk...


July 1, 2001

commentcount version 2.1

No doubt in reaction to my naming them host-of-the-week, BarrysWorld has been up and down like..., like a free PHP host ever since. At least it finally pushed me into figuring out a better way to do comment counting, allowing the page to completely load before the comment count attempts to contact your remote PHP host. For version 2.1, your commentcount.php should be:

<?php
$output = "var commentID = new Array(); var commentCount = new Array();";
if ($dir = opendir("comments")) {
 $i = 0;
 while($file = readdir($dir)) {
  if ($file != "." && $file != ".."){
   $comment_number = explode(".", $file);
   $thisFile = file("comments/" . $file);
   $comments = sizeof($thisFile);
   if($comments == 1) {$comments .= " comment";} else {$comments .= " comments";}
   $output .= "commentID[" . $i . "]=\"comment" . $comment_number[0] . "\"; commentCount[" . $i . "]=\"" . $comments . "\";";
   $i++;
   }
  }
  closedir($dir);
 }
echo $output;
?>

In the <head> section of your template, include these two scripts:

<script type="text/javascript" src="http://www.youraccount.barrysworld.net/path/commentcount.php" defer="defer"></script>

<script type="text/javascript">
function countcomments(){
 if (document.getElementById){
  if(typeof(commentID) != "undefined"){
   for (i=0;i<commentID.length;i++){
    oSpan = document.getElementById(commentID[i]);
    if(oSpan){
     oSpanText = oSpan.firstChild;
     while(oSpanText.nodeType != "3"){
      if(oSpanText.hasChildNodes)
       oSpanText = oSpanText.firstChild;
      else
       return false;
      }
     oSpanText.nodeValue = commentCount[i];
     }
    }
   }
  }
 }
</script>

In your <body> tag, include onload="countcomments()" so that you have something like:

<body link="#336699" vlink="#003366" alink="red" onload="countcomments()">

And finally, replace the link text for the comment link in your byline with:

<span id="comment<$BlogItemNumber$>">comment</span>

So that the whole link looks something like:

<a href="javascript:viewComments(<$BlogItemNumber$>)"><span id="comment<$BlogItemNumber$>">comment</span></a>

Now what will happen is that your page will load with the comment link saying just "comment", and then try to get commentcount.php from your remote PHP host. If your remote host is down, eventually the browser gets tired of waiting, and nothing happens (until you click a "comment" link and get a page not found error). If your remote host returns commentcount.php, and if you are using a DOM compliant browser (IE 5+, Netscape 6, sometimes Opera), then once the page is completely loaded the "comment" links will be rewritten with the number of comments for posts that have comments, or left as just "comment" when there are no comments.



June 30, 2001
Remote dotcomments host-of-the-week is BarrysWorld. It's another online gaming server, like teamgaming.net, but unlike teamgaming, BarrysWorld offers FTP access, and has mail() enabled and troublefree. So here's my recommendation for getting remote dotcomments going, as of today:


<a href="http://www.foreword.com/dotcomments.php"><img src="http://www.youraccount.barrysworld.net/path/dcbanner.gif" width="88" height="31" border="0" alt="Comments by dotcomments"></a>



June 11, 2001
Looks like you can scratch digitalrice off your list of free PHP web hosts. According to Mike's post in the forum, "PHP and SSI will be disabled once the server comes back online. They might possibly be added back in the future, but they are disabled for security purposes." Rats.

Looking at 4Nx2 last night, I learned what may be the reason why there are so few free hosts with PHP enabled. Apparently it's very handy for spammers, who put a "remove" link in the spam, with your email address in the query string, and then use PHP to read it ($sucker[i]=$QUERY_STRING). I didn't get around to trying to sign up with them, to see if dotcomments would work with their "several functions are disabled" setup, but it's worth a try if you can't get in at f2s or portland.co.uk/port5.com/isamillionaire.com.



June 10, 2001
This may not be the most stable way to get comments. First I was using digitalrice, but then it went down for what they said would be a week or so. Moved to port5, which worked for about a day, and is now not working (perhaps I got TOSsed for too much remote traffic, since I was just installing it on archives.blogspot.com when Ev decided to put me on the front page. Nice live stress-test, but the comments failed). f2s isn't taking new signups. I would switch to xoasis, despite the popups, but the popup kills the comment count. Rats. As of this second, digitalrice is back up, port5 is back up, so back to them we go.

If you want the comment count, and you use tables for your page layout, this might be a good time to switch to CSS instead. If you have tables, the browser has to know how big everything inside the table will be before it will display anything. That means that the comment count script for every single post has to run before any part of your blog shows up. With CSS layout, the first post displays, then its comments are counted, then the second post displays, then its comments are counted... much more viewer friendly.



June 6, 2001
Want to use dotcomments in your blog, but your host doesn't support PHP? Phil Ulrich figured out how to host it on another server that does support PHP (like f2s, port5, or digitalrice), but the comment count, which requires PHP in your blog itself, didn't work. Once you've got dotcomments set up according to Phil's instructions, here's how to get the comment count working:

[Update: version 2.1 (above) is much faster loading, smoother, and more tolerant of remote host downtime.]

Copy the code below, paste it into a text editor, save the file as commentcount.php, and upload it to your PHP server.

<!-- Begin copying -->
<?php
$n = $QUERY_STRING;
if ($file = @fopen("comments/$n.comment", 'r')) {
 $thisFile = file("comments/$n.comment");
 $comments = sizeof($thisFile);
 if($comments == 1) {$comments .= " comment";} else {$comments .= " comments";}
 }
else {$comments = "0 comments";}
echo "document.write('$comments');";
?>
<!-- End copying -->

Next, replace the text for your comment link in your blog template (Discuss, comment, whatever you used) with the following line, and be sure to replace http://www.example.com/path/ with your domain and path to commentcount.php:


<!-- Begin copying -->
<script type="text/javascript" language="JavaScript" src="http://www.example.com/path/commentcount.php?<$BlogItemNumber$>"></script>
<!-- End copying -->

Save changes, publish, and you should be counting comments!