Login | Register

Nerd Paradise

↑ ↑ ↓ ↓ ← → ← → B A start

A Fully Function PHP Forum in 964 Bytes of Code

Post by: Blake
Posted on: 10 Cado 7:0 - 19.92.85
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.
facebook twitter Stumbleupon Reddit del.icio.us Digg
User Comments: 7
Post by beary605 on 11 Ineo 1:4 - 18.32.36
Dang! That's one trolled forum!
Post by lucb1e on 11 Ineo 2:5 - 9.52.4
Quite amazing, only a name field actually. Of course an account system and boards should be included for a decent forum, but for a basic one this+name field is enough (imo) :)

Oh i forgot to mention my comments on the code (only commented on functionality above, please merge if possible):
  • <? does not work as php initiator for me (maybe another version, i've seen your way before), <?php[space] made it work;
  • You can leave the action property in the form, it defaults to the current page;
  • The / in <tag/> isn't needed, it won't pass a validator without doctype regardless, and it works without so why bother to use the /'es;
  • And while we're ruining the bit of validatity it had, we might remove html and body tags as well :+)
  • Rather than f.php you could have used ./ to point to the current directory. The page name should be index.php or similar though;
  • Because of the removal of the post action, it posts to ./?v=x already, and the $_REQUEST uses the url parameter to get it's V, no hidden element needed for that.

With those changes, it becomes 873 bytes. Using username u and using a password of 7 characters (sufficient if you make the server slower so brute force won't work :p) it even becomes 865... Hmm I'm not going to dive any further into the code, but maybe if you assume the tables exist, you might even include a user field and stay under 1kb :)

Doublepost fixified. The edit button is your friend.
Post by lucb1e on 11 Ineo 3:2 - 12.91.41
Well if there was any edit button I would have used it... Same goes for the 'email me' or 'contact admin' option, which appears to be missing too.

(FF3.6, shouldn't break styles making edit-button-elements magically disappear...)
Post by Never on 11 Ineo 4:0 - 4.84.31
But there IS an edit button...
Post by RainingOnYourParade on 11 Ineo 4:1 - 16.84.38
The [Flag] button is acceptable to use if you want to get a mod's attention so they can delete a double post or whatever, as well.
Post by lucb1e on 11 Ineo 5:3 - 0.3.1
Ah blogposts are also in the forum! Now I get how you see that there is a new comment, and how you can edit :P I was only seeing this at first, and had no clue where to find an edit button: http://updo.nl/file/826ea049.png (I was logged in there).
Post by phpcompressorman on 11 Cado 0:1 - 12.98.47
I wanted to share that I have updated this script for some "crowd-control" to use IP logging, DNSBL checking, and fixed E_NOTICE errors. Still only 1024 bytes.

You must be logged in to add a comment
Current Date: 16 Vigeo 13:3Current Time: 3.38.51Join us in IRC...
Server: irc.esper.net
Channel: #nerdparadise
Your IP: UnknownBrowser Version: 0