Chris Hager
Programming, Technology & More

Most Simple Ajax Chat Ever

Most Simple Ajax Chat Ever

A fun little project from January 2006. Back then ajax just started becoming popular, but with very few tools around. jQuery came out later that year, Github just two years later, in 2008.

Features

  • Super simple
  • Efficient spam filter (thanks to some live ‘hackers’ at launch)
  • Only 2 files needed (index.html, w.php)
  • No libraries at all (using pure XMLHttpRequest)
  • CSS styles & usernames

How it works

  • index.html reads the content & sends your message to w.php
  • w.php writes the content and prunes it to $maxlines lines
  • Content stored as text in chat.txt
  • 2 http_requests: 1 for checking content, 1 for sending your message

Setup in 2 Steps

  • Copy index.html and w.php in a directory of your choice
  • Create a file called chat.txt (writeable for php)

License: MIT

Source of w.php:

 1 <?php
 2     /**
 3      * Author: chris at linuxuser.at
 4      * Licence: MIT
 5      */
 6 
 7     $fn = "chat.txt";
 8     $maxlines = 20;
 9 
10     $nick_maxlength = 10;
11 
12     /* Set this to a minimum wait time between posts (in sec) */
13     $waittime_sec = 0;
14 
15     /* spam keywords */
16     $spam[] = "cum";
17     $spam[] = "dick";
18 
19     /* IP's to block */
20     $blockip[] = "72.60.167.89";
21 
22     /* spam, if message IS exactly that string */
23     $espam[] = "ajax";
24 
25     $msg = $_REQUEST["m"];
26     $n = $_REQUEST["n"];
27 
28     if ($waittime_sec > 0) {
29         $lastvisit = $_COOKIE["lachatlv"];
30         setcookie("lachatlv", time());
31 
32         if ($lastvisit != "") {
33             $diff = time() - $lastvisit;
34             if ($diff < $waittime_sec) { die(); }
35         }
36     }
37 
38     if ($msg != "") {
39         if (strlen($msg) < 2) { die(); }
40         if (strlen($msg) > 3) {
41             /* Smilies are ok */
42             if (strtoupper($msg) == $msg) { die(); }
43         }
44         if (strlen($msg) > 150) { die(); }
45         if (strlen($msg) > 15) {
46             if (substr_count($msg, substr($msg, 6, 8)) > 1) { die(); }
47         }
48 
49         foreach ($blockip as $a) {
50             if ($_SERVER["REMOTE_ADDR"] == $a) { die(); }
51         }
52 
53         $mystring = strtoupper($msg);
54         foreach ($spam as $a) {
55              if (strpos($mystring, strtoupper($a)) === false) {
56                  /* Everything Ok Here */
57              } else {
58                  die();
59              }
60         }
61 
62         foreach ($espam as $a) {
63             if (strtoupper($msg) == strtoupper($a)) { die(); }
64         }
65 
66         $handle = fopen ($fn, 'r');
67         $chattext = fread($handle, filesize($fn)); fclose($handle);
68 
69         $arr1 = explode("\n", $chattext);
70 
71         if (count($arr1) > $maxlines) {
72             /* Pruning */
73             $arr1 = array_reverse($arr1);
74             for ($i=0; $i<$maxlines; $i++) { $arr2[$i] = $arr1[$i]; }
75             $arr2 = array_reverse($arr2);
76         } else {
77             $arr2 = $arr1;
78         }
79 
80         $chattext = implode("\n", $arr2);
81 
82         // Last spam filter: die if message has already been in the chat history
83         if (substr_count($chattext, $msg) > 2) { die(); }
84 
85         $spaces = "";
86         if (strlen($n) > $nick_maxlength-1) $n = substr($n, 0, $nick_maxlength-1);
87         for ($i=0; $i<($nick_maxlength - strlen($n)); $i++) $spaces .= " ";
88 
89         $out = $chattext . $n . $spaces . "| " . $msg . "\n";
90         $out = str_replace("\'", "'", $out);
91         $out = str_replace("\\\"", "\"", $out);
92 
93         $handle = fopen ($fn, 'w'); fwrite ($handle, $out); fclose($handle);
94     }
95 ?>
swirl