PHP-TypeKey
Synopsis
An implementation of TypeKey in PHP, for use with Movable Type weblogs.
Last updated
2005-01-08
Contents
Introduction
This is a guide to altering your Movable Type archive pages so that the TypeKey code is displayed using PHP instead of JavaScript. There are three main advantages to doing this:
- Faster page loading. To check whether the user is signed in using TypeKey, a piece of JavaScript calls a CGI script (specifically a function of mt-comments.cgi) to get the value of a cookie. As Perl can be quite slow, this slows down the page. Using PHP to get the cookie is much faster.
- More accessible. Because text browsers like Lynx and screen readers cannot usually understand JavaScript, you will be allowing clients that normally could not use TypeKey to use it. It will also work for users who have voluntarily disabled JavaScript.
- Better compatibility with XHTML. If you use XHTML and want to send your pages using the correct MIME type (which is
application/xhtml+xmlinstead oftext/html), you will be unable to use thedocument.write()JavaScript functions which the original implementation relies on. Because PHP-TypeKey does away with the JavaScript, you won't have this problem.
Version History
Version 0.2 (2005-01-08)
- Now includes code for blogs where comment registration is required.
- No longer requires the SafeHref plugin for ensuring that the HTML produced is valid - instead uses the
htmlentities()function in PHP (thanks to Mike Wills) - More compatible with dynamic publishing (thanks to Arvind Satyanarayan).
- No more JavaScript errors when changing the 'Remember Me?' options.
Version 0.1 (2004-11-17)
- Initial release
Implementation Details
If you want to use PHP-TypeKey on your own site, you have two options:
- Use this Individual Entry Archive template. This is the same as the template offered at movabletype.org, but with the PHP code inserted. Use this if you are still using the default template or have only made minor modifications and don't mind re-implementing them.
- Modify the code manually, by following the instructions below.
Requirements
For PHP-TypeKey to work, you need to do the following first:
- Ensure you're running a minimum of Movable Type 3.0D (a later version like 3.14 is also fine). This won't work on Movable Type 2.x or 1.x because TypeKey isn't supported in those versions.
- Ensure that the files you are outputting are PHP. If you're using dynamic publishing then you're okay since this is done using PHP anyway, but if not, you have two options:
- In Movable Type, open your blog and select 'Weblog Config'. Choose the 'Preferences' tab, and then scroll down to 'Archiving Preferences'. In 'File Extension for Archive Files:', change this to 'php'. Note that this will break your permalinks.
- Configure your web server to output .html files as PHP. You will need to create a file called .htacces in your blog's directory (if there already is one, modify it) and add the line AddHandler application/x-httpd-php .html. Now, all of your HTML files will be treated as PHP.
- Ensure (there's quite a bit of ensuring...) that you have TypeKey enabled and that it is working, by way of a TypeKey token.
Below is the code you will need to alter to use PHP-TypeKey.
Find the following block of code:
<div id="thanks">
<p>Thanks for signing in,
<script type="text/javascript" src="<MTCGIPath>
<MTCommentScript>?__mode=cmtr_name_js"></script>
<script>document.write(commenter_name);</script>.
Now you can comment. (<a href="<$MTRemoteSignOutLink static="1"$>">sign out</a>)</p>
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)
<form method="post" action="<$MTCGIPath$><$MTCommentScript$>" name="comments_form" onsubmit="if (this.bakecookie[0].checked) rememberMe(this)">
<input type="hidden" name="static" value="1" />
<input type="hidden" name="entry_id" value="<$MTEntryID$>" />
<p><label for="url">URL:</label><br />
<input tabindex="1" type="text" name="url" id="url" />
Remember me?
<input type="radio" id="remember" name="bakecookie" /><label for="remember">Yes</label><input type="radio" id="forget" name="bakecookie" onclick="forgetMe(this.form)" value="Forget Info" style="margin-left: 15px;" /><label for="forget">No</label><br style="clear: both;" />
</p>
<p><label for="text">Comments:</label><br />
<textarea tabindex="2" id="text" name="text" rows="10" cols="50"></textarea></p>
<div align="center">
<input type="submit" tabindex="3" name="preview" value=" Preview " />
<input style="font-weight: bold;" tabindex="4" type="submit" name="post" value=" Post " />
</div>
</form>
</div>
<script language="javascript" type="text/javascript">
<!--
if (commenter_name) {
document.getElementById('thanks').style.display = 'block';
} else {
document.write('You are not signed in. You need to be registered to comment on this site. <a href="<$MTRemoteSignInLink static="1"$>"> Sign in</a>');
document.getElementById('thanks').style.display = 'none';
}
// -->
</script>
Now, replace it with the following code:
<div id="thanks">
<?php
$commentername = $_COOKIE["commenter_name"];
$url=$_COOKIE["mtcmthome"];
if ($commentername)
{
?>
<p>Thanks for signing in, <?php echo($commentername); ?>.
Now you can comment. (<a href="<?php echo(htmlentities('<$MTRemoteSignOutLink static="1"$>')); ?>">sign out</a>)</p>
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)
<form method="post" action="<$MTCGIPath$><$MTCommentScript$>" name="comments_form" onsubmit="if (this.bakecookie[0].checked) rememberMe(this)">
<input type="hidden" name="static" value="1" />
<input type="hidden" name="entry_id" value="<$MTEntryID$>" />
<p><label for="url">URL:</label><br />
<input tabindex="1" type="text" name="url" id="url" />
Remember me?
<input type="radio" id="remember" name="bakecookie" /><label for="remember">Yes</label><input type="radio" id="forget" name="bakecookie" onclick="forgetMe(this.form)" value="Forget Info" style="margin-left: 15px;" /><label for="forget">No</label><br style="clear: both;" />
</p>
<p><label for="text">Comments:</label><br />
<textarea tabindex="2" id="text" name="text" rows="10" cols="50"></textarea></p>
<div align="center">
<input type="submit" tabindex="3" name="preview" value=" Preview " />
<input style="font-weight: bold;" tabindex="4" type="submit" name="post" value=" Post " />
</div>
</form>
</div>
<?php
}
else
{
echo('You are not signed in. You need to be registered to comment on this site. <a href="' . htmlentities('<$MTRemoteSignInLink static="1"$>') . '"> Sign in</a>');
}
?>
Next, find the following block of code:
<script type="text/javascript" src="<MTCGIPath><MTCommentScript>?__mode=cmtr_name_js"></script>
<script language="javascript" type="text/javascript">
<!--
if (commenter_name) {
document.write('Thanks for signing in, ', commenter_name, '. Now you can comment. (<a href="<$MTRemoteSignOutLink static="1"$>">sign out</a>)');
} else {
document.write('If you have a TypeKey identity, you can <a href="<$MTRemoteSignInLink static="1"$>"> sign in</a> to use it here.');
}
// -->
</script>
</MTIfNonEmpty>
<form method="post" action="<$MTCGIPath$><$MTCommentScript$>" name="comments_form" onsubmit="if (this.bakecookie[0].checked) rememberMe(this)">
<input type="hidden" name="static" value="1" />
<input type="hidden" name="entry_id" value="<$MTEntryID$>" />
<div id="name_email">
<p><label for="author">Name:</label><br />
<input tabindex="1" id="author" name="author" /></p>
<p><label for="email">Email Address:</label><br />
<input tabindex="2" id="email" name="email" /></p>
</div>
<MTIfNonEmpty tag="MTTypeKeyToken">
<script language="javascript" type="text/javascript">
<!--
if (commenter_name) {
document.getElementById('name_email').style.display = 'none';
}
// -->
</script>
</MTIfNonEmpty>
And replace it withe following code:
<?php
$commentername = $_COOKIE["commenter_name"];
if ($commentername)
{
echo('Thanks for signing in, ' . $commentername . '. Now you can comment. (<a href="' . htmlentities('<$MTRemoteSignOutLink static="1"$>') . '">sign out</a>)');
}
else
{
echo('If you have a TypeKey identity, you can <a href="' . htmlentities('<$MTRemoteSignInLink static="1"$>') . '"> sign in</a> to use it here.');
}
?>
</MTIfNonEmpty>
<form method="post" action="<$MTCGIPath$><$MTCommentScript$>" name="comments_form" onsubmit="if (this.bakecookie[0].checked) rememberMe(this)">
<input type="hidden" name="static" value="1" />
<input type="hidden" name="entry_id" value="<$MTEntryID$>" />
<MTIfNonEmpty tag="MTTypeKeyToken">
<?php
if ($commentername)
{
?>
<input type="hidden" id="author" name="author" />
<input type="hidden" id="email" name="email" />
<?php
}
else
{
?>
<div id="name_email">
<p><label for="author">Name:</label><br />
<input tabindex="1" id="author" name="author" /></p>
<p><label for="email">Email Address:</label><br />
<input tabindex="2" id="email" name="email" /></p>
</div>
<?php
}
?>
</MTIfNonEmpty>
Now, rebuild your Individual Entry Archives, and you're done!
Comments and Questions
If you have any comments or questions about this code, then please feel free to get in touch with me via email - my address is 'neil' at neilturner.me.uk. I'll do what I can to answer your questions.