MT Search and PHP Includes

Step 1

First create your new search results page with as many PHP includes as necessary. Save it as PHP in a directory outside of the cgi-bin—wherever you keep your other files. Insert the following code where you would like the search results to appear:

// Change $file to the full url to your MT cgi search script

Step 2

Now set up the default search template to output just the results of the search—not an entire HTML document. These HTML nuggets will plug into the new search results page you just created

Step 3

Then point your search form(s) to the new search results page and make sure they use the “get” method.

That’s all there is to it. The PHP function readfile()—surprise!—reads in the results of the CGI script which receives the search query string.

One tiny drawback

Unfortunately you lose the entry “Edit” link. Sure, that link is convenient but it’s a small price to pay for never having to worry about the navigation or design of your search results page becoming un-synced with the rest of your site.

Blue Collar Design
Number Seven
Shaun Inman
January 31st, 2004 at 8:20 pm
Movable Type
005 (Now closed)



Shaun, this is an interesting approach. I was looking for some time now to see how is it possible to combine PHP with Movable Type’s CGI. There are two cases where this is needed; the search results and the comments pages. These are the only pages MT requires CGI. I searched the internet but I couldn’t come up with a solution, so I tried to solve the problem myself.

As far as I can remember (since I stopped using MT some time ago in favour of Drupal), the trick is easier than you think. At least it worked for me…

There is a function in PHP called ‘virtual()’. This is the that does the trick. The steps are easy:

1.) You modify all your search forms to point to a new search page instead of the standard CGI search page used by MT. 2.) You create this new search page which is formatted the way you want it (with PHP includes and any other PHP scripts you have) which also produces the search results by calling the virtual function to the actual MT CGI search script: virtual(/cgi-bin/mt/mt-search.cgi?search_query); Because you already have modified your search forms to point to the PHP search page, passing the query string is a piece of cake with a GET/POST function. In some server configuration you might have to include the virtual file in quotes: virtual(‘/cgi-bin/mt/…’); In some other cases you will have to use the entire path instead of the relative. In any case have to change the path to suit your MT installation. I don’t really remember if the output was formatted or not (I’m an XHTML maniac) but even if it is not formatted (or produces invalid XHTML) you can always change a couple of lines in the original MT CGI script.

The same can be done with the comments pages and the rest of the cases where CGI scripts need to be called through PHP pages.

Sorry if my english confuse you. I’m available if you need any extra help. If you come up with a better solution, please let me know.

M. Perakis
May 25th, 2004 11:31 am

Did I mention why virtual instead of readfile? Sorry, I forgot… The readfile() has two disadvantages: a.) it consumes a lot of memory on the server, infact it consumes the size of the file in memory! b.) some hosting providers (many) do not allow calls to this function. I read somewhere why, but I don’t remember.

We say excactly the same thing, the only difference is the function call. After 10 long hours at work with all this heat here in Greece, I don’t understand what I’m reading. I shouldn’t have written all this. Sorry.

Try virtual instead of readfile and tell me if it works. I’ll check that edit link thing as long as MT go free again (probably never)…

M. Perakis
May 25th, 2004 12:18 pm

This should come very much in handy… thanks!

May 25th, 2004 4:50 pm

using the virtual() function instead of readfile() works great and seems to be a bit faster as well. Just remember that you are calling the actual file on the server and not an http request like you would when using the readfile method, so in Shuan’s example you would change the path of $file to “../cgi-bin/mt-search.cgi” if you are calling the search results from “”

joe mcgill
Jul 11th, 2004 2:30 pm

I ran into some problems because readfile() does not work on my hosting provider (DreamHost) because, allow_url_fopen is set to (off) in the php.ini.

To change php.ini I would have to build my own local version of PHP. I took the basic precept of your method, and implemented it this way:

I set it up to:

  1. use a search page located at /search/index.php
  2. place IncludeBlogs= in one location outside of <form>.

The method:

  1. The form code:

    <form method="get" action="">
        <input value="Site Search" name="search" id="search" >
        <input type="submit" value="Search" />
  2. The code to use in /search/index.php to show output of mt-search.cgi

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                "IncludeBlogs= blog id # you want to search&IncludeBlogs=blog
    id # you want to search&$_SERVER[QUERY_STRING]");
  3. For /path_to_your_mt/search_templates/default.tmpl strip and use desired <MTSearchResults> tags and form tags.

See it working here.

Parker Brooks
Jun 24th, 2005 6:13 am