Work around for MT Comments CGI with PHP
Posted In: Movable Type, Tutorial
- Movable Type
The blog engine I use for this site is Movable Type, it's very flexible and scalable and meets my needs quite nicely. I did however hit a few problems when integrating it, specifically with comments. In this blog I share my solution to the problem I had.
Movable Type is built using Perl and for the most part that is fine, its logic (Perl) is well separated from its presentation (HTML).
MT allows you to specify what extension files are published as, so as is the case for this site, I specified PHP. PHP is the language I know, and I use various includes throughout the site as well as a few other scripts such as phpFlickr to generate my Flickr stream.
The only problem I came across when integrating MT into this site was with two scripts in particular.
- Comments Response - mt-comments.cgi
- Search Results - mt-search.cgi
You can change the HTML for these "elements" through the MT System Templates section. Problems arise when you try and integrate these “elements” into a PHP page.
mt-search.cgi Problem
I'll start with mt-search.cgi because it was the easiest to fix, mainly because I didn't have to think of a solution. Shaun Inman already had, and his article on the matter can be found here.
To summarise, both mt-comments.cgi and mt-search.cgi are passed variables and return a result. However they don't return the result as a variable passed to the template engine. They return them directly from the CGI script i.e. they generate the result directly through the CGI and CGI and PHP don't mix.
I won't repeat what Shaun has already written so take a look at his article (it's not very long).
There was just one other addition I made to his solution. For some reason the mt-search.cgi script has a 60 second timer between searches and I kept getting an error message along the lines of “error, search is already in progress”. I fixed this simply by adding the following line into mt-config.cgi file:
ThrottleSeconds 1
This sets the timer to 1 second and seems to have alleviated that issue.
mt-comments.cgi Problem
My big problem was with mt-comments.cgi and unfortunately I couldn't find a solution through Google. So I had to actually sit and think about it.
The problem arises when a visitor submits a comment. By default the comment form POSTs the submitted variables to mt-comments.cgi. mt-comments.cgi then processes the submission and displays the results, typically a success or failure. Because the script itself returns the result, and obviously the script is Perl, you cannot have any PHP code as well. In my case this meant no includes, no Flickr stream etc. Clearly a problem.
Initially you would think that Shaun Inman's solution for the search script would be the logical solution however, Shaun's solution assumes that the script is expecting GET variables, i.e. variables appended to the end of the URL. The comments script however is expecting POST variables. So appending the variables to the end of the URL throws up an error. His solution won't work here.
My Solution
To fix this problem we need the comments form to POST to a PHP page and for the PHP page to re-POST the variables to the mt-comments.cgi script. The mt-comments.cgi script will them return it results to the PHP script and we can display those results through PHP.
Step 1
To facilitate the re-POSTing of the variable we are going to use a PEAR package called HTTP_Request. A lot of hosting providers already have PEAR packages install, if you have a cPanel there is usually a link that tells you what packages are already installed for you.
If not you can download and install the package locally within your site but that is beyond the scope of this tutorial. Visit pear.php.net for details of how to do this.
Assuming you have HTTP_Request installed let's continue.
Create a new template in MT under the Index Templates, and set its to Output File to be comments.php. Add in whatever HTML you want to make the template fall inline with the rest of your site.
Where your want the Comment Response message to appear add the following code:
<?php
require_once "HTTP/Request.php";
$req = new HTTP_Request("http://www.yoursite.co.uk/cgi-bin/mt/mt-comments.cgi");
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addPostData("author", $_POST['author']);
$req->addPostData("email", $_POST['email']);
$req->addPostData("url", $_POST['url']);
$req->addPostData("text", stripslashes($_POST['text']));
$req->addPostData("post", $_POST['post']);
$req->addPostData("static", $_POST['static']);
$req->addPostData("entry_id", $_POST['entry_id']);
$req->addPostData("__lang", $_POST['__lang']);
if(!PEAR::isError($req->sendRequest())){
echo $req->getResponseBody();
}
?>
Looking through this code you can see that we include the HTTP_Request package. Then create a new object which points at the mt-comments.cgi file. We set the method to POST because we want to POST the variables to the mt-comments.cgi, and then we add the POST variables.
Remember we are going to POST to comments.php instead of directly to mt-comments.cgi.
Finally the if statement is just some error checking to make sure everything went ok.
Step 2
Find the Comment Form template under the Template Modules section.
Change the form action to point to the new file you created, e.g. http://www.yoursite.com/comments.php. And that should be it.
Publish your site and try leaving a comment. You can now use PHP within your comments.php file with no conflicts.
Conclusion
I'm not saying this is the best solution to this problem. It's just one that has worked for me. If anyone has a better way of fixing this I would be interested to know what it is.
Hope this helps. Let me know your thoughts.
Quotes
Never take the advice of someone who has not had your kind of trouble. - Sidney J. Harris
Tech Read
A great reference book for learning jQuery, broken into sensible chapters focused on jQuery's core functions.
Leisure Listen
Book 1 of A Time Odyssey, Arthur C. Clarke's final trilogy before passing away. Time's Eye is an interesting idea, but not his strongest novel unfortunately.





