Fed up once and for all with the lackluster reliability and performance of the “free” commenting systems in use by some of my friends, I decided to see if I could rig it so (in theory) they could use a comment script hosted here at greyduck.net instead.
So what do you do when you want to reference a comment-counting feature that’s written in PHP, on another server, from a server on which PHP is not installed? You call it as if it was a Javascript, of course! (And by “of course” I mean “I learned after a solid hour of Googling and headscratching”.)
Here’s the trick:
<script language="JavaScript" src="count.php?id=X">
X gets filled in with the journal/blog/thingie entry number via whatever mechanism your journal software provides, and the ‘count.php’ is configured not to ‘return’ its calculated result but instead:
echo "document.write("$result")";
In other words, PHP is generating what looks like Javascript, and everybody’s happy. My hacked-together ‘count.php’ looks like this (with some obfuscation): function commentCount($n) { echo "document.write("$comments")"; commentCount($id); ?>
<?
$comments_path = "/path/to/comments/directory/";
if($file = @fopen($comments_path . "comments/$n.comment", 'r')) {
$thisFile = fread($file, '1000000');
$thisFile = explode("n", trim($thisFile));
fclose($file);
$comments = sizeof($thisFile);
if($comments == 1) {$comments .= " comment";} else {$comments .= " comments";}
} else {
$comments = "no comments yet";
}
}
And the link that calls the comment script and displays a live count of submitted comments looks like:
<A HREF="javascript:viewComments(X)"><script language="JavaScript" src="http://www.domain.net/location/count.php?id=X">
Again remembering to replace that X with the unique identifier for that journal/thingie/blog entry. The viewComments() function is your standard “new window popup” trick that I’m sure nobody needs me to detail here. (The X in that function call is passed to the URL for the comment script proper. Nuff said.)
Granted that this is all designed around the comment code used on this website, so it’s not exactly portable for all uses. The code listed here is only an example of how it can be done.
Not, mind you, that I’m putting this system into service right away… but working this out certainly was an interesting learning experience for me! Yay, my geek-fu is still strong! *smirk*
UPDATE: I’m a freakin’ moron. It works great in Mozilla-based browsers but Internet Explorer ignores the living hell out of my count.php script. I have no idea why. I’ve added content-type headers, I’ve tinkered with it six ways from Sunday, and still no-go. So, basically, this was an exercise in utter futility. Argh.
Comments
5 responses to “External PHP via Javascript”
Ok, that’s all cool and everything. But now you need to show me how to do it for my thingie. Because I’m a boob.
Dawn found something that may do the trick. I was just too frustrated and frazzled last night to tinker with it, but essentially somebody packaged a variant of dotcomments specifically to solve the “what if my website doesn’t do PHP?” problem. (I’ll let the interested parties know as soon as I know one way or the other.)
At least I learned a few new things, even if the eventual result is a bit underwhelming.
Two things.
1. IE is notorius at not following some of the web’s conventions. Something I don’t think I need to remind you of.
II. It’s still a brilliant idea. But I can understand your frustration.
Is it any wonder I want to grow up to be just like you?
Konqueror doesn’t like it either, sad to say. Opera, anyone?
http://gxp.entercomradio.com/test/
The link will be “useful” for at least the next few days…
Ever the faithful Opera user, I am sad to say it doesn’t show the link. sign me up if you get t working though!