Blake O'Hare .com

A Fully Function PHP Forum in 964 Bytes of Code

Someone in the IRC room spoke of a forum that was once written in under 2 KB of code. A simple minimalistic forum that had no user account system, but still had enough functionality to support the notion of threads and posts.

Almost jokingly, I said I bet I could do it in 1KB. But I gave it a shot anyway, and behold, here it is, slightly under 1KB at 964 bytes!

<html><body><h1>1KB Forum</h1><?mysql_connect('localhost','username','********');mysql_select_db('d');extract($_REQUEST);$v=intval($v);$i=0;$q='mysql_query';$f='mysql_fetch_row';$n='mysql_num_rows';$x='<input type="';$s="SELECT*FROM";$t='CREATE TABLE IF NOT EXISTS t(i INT AUTO_INCREMENT,a INT,b TEXT,KEY(i))';$h='htmlspecialchars';$q($t);$q(str_replace('t','p',$t));$l=' ORDER BY';$o='';$u='INSERT INTO';$c="b)VALUES('";if($b){if(!$v)$q("$u t($c$e')");$v=max($v,mysql_insert_id());$q("$u p(a,$c$v','$b')");}if($v){$t=$q("$s p WHERE a=$v$l i");echo'<a href="f.php">Back</a>';for(;$i<$n($t);++$i){$r=$f($t);echo'<hr/>'.nl2br($h($r[2]));}}else{$t=$q("$s t$l-i");for(;$i<$n($t);++$i){$r=$f($t);echo'<a href="f.php?v='.$r[0].'">'.$h($r[2]).'</a><br/>';}$o='Title:'.$x.'text"name="e"/><br/>';}echo'<hr/>Post:<form action="f.php"method="post">'.$x.'hidden"name="v"value="'."$v\"/>$o<textarea name=\"b\"></textarea>$x";?>submit"name="w"value="Post"/></form></body></html>


It even creates the schema the first time you access the script. Of course you have to first have a database called 'd' and update the MySQL account credentials at the beginning to match your own. Magic quotes need to be enabled to prevent SQL injection!

I have this script running here if you want to see what it looks like in action. You'll have to pardon the graffiti, though. As you can imagine there is no such thing as a moderator on this forum.