1. Home > programming >

A php+ajax real-time chat room worth sharing

A very classic php+ajax real-time chat room, which uses PHP files to save chat records, divided by day, PHP has only one file for chatting, and integrates PHP and AJAX technology, which means that you only need to run this one The file can start the PHP chat room. The code above is also very simple, but it has realized the general functions of the chat room, the nickname during chat, the color of the nickname, the size of the chat font, the font, the bold, the change of the form Big to small, etc. If you want to create a chat room for fun, this source code can fully meet ordinary needs.

The specific effect is as follows:

Key code:

 <  ?php header('content-type:text/html;charset=utf-8'); //Display online users $disOnline = 1; //Display the number of recent content when logging in (the default is 30  ) $leastnum = 30; //The default room name (the default is to change a file every day), if d is removed, it is to change a file every month $room = date("Ymd"); //The saving path of the room must be imitated  quot;/" at the end, you can quot;../", etc. $roomdir = "rooms/"; //Encoding method $charset = "UTF-8"; //The maximum number of content displayed on the client side (not too  Large) $maxdisplay = 300; //Language package $lang = array( //Chat room description "description"=>"Chat room.", //Chat room title "title"=>"Welcome...!",  //The first one to welcome to the chat room"firstone"=>"Welcome...!", //Display "ban" when the message has prohibited content => array('Falungong',  'Communist Party','Li Hongzhi','fuck','叼','Fuck','Fuck'), //Keywords "keywords"=>"Welcome...!", //Speaking tips  "hereyourwords" => "Speak here!" ); $touchs = 10; $title = $lang["title"]; $earlier = 10; $description = $lang["description"]; $origroom = $room  ; $least = ($_GET["dis"])?intval($_GET["dis"]):$leastnum; if ($_GET["room"]) $room = $_G  ET["room"]; $room = checkfilename($room); if (!$room) $room = $origroom; $filename = $roomdir.$room.".dat.php"; $datafile = $roomdir.  $room.".php"; if (!is_dir($roomdir)) {@mkdir($roomdir, 0777) or exit('no this dir.');} if(file_exists($filename)){ if ((  int)filemtime($filename) + 1800 '."\n".time()."|".$lang["firstone"]."\n"); if (!file_exists($datafile)) @file_put_contents($datafile,'<  ?php die();?>'."\n"); $action = $_GET["action"]; if (!function_exists("file_get_contents")) {function file_get_contents($path)  {if (!file_exists($path)) return false; $fp=@fopen($path,"r"); $all=fread($fp,filesize($path)); fclose($fp); return $  all;}} if (!function_exists("file_put_contents")) {function file_put_contents($path,$val) {$fp=@fopen($path,"w"); fputs($fp,$val); fclose(  $fp); return true;}} function checkfilename($file) {if (!$file) return ""; $file = trim($file);  $a = substr($file,-1); $file = eregi_replace("^[.\\\/]*","",$file); $file = eregi_replace("[.\\\/]*  $","",$file); $arr = array("../","./","/","\\","..\\",".\\"); $  file = str_replace($arr,"",$file); return $file;} function get_ip() {global $_SERVER; if ($_SERVER) {if ($_SERVER[HTTP_X_FORWARDED_FOR]) $realip = $_SERVER["HTTP_X_FORWARDED_FOR  "]; else if ($_SERVER["HTTP_CLIENT_ip"]) $realip = $_SERVER["HTTP_CLIENT_ip"]; else $realip = $_SERVER["REMOTE_ADDR"];} else {if (getenv('HTTP_X_FORWARDED_FOR')) $  realip = getenv('HTTP_X_FORWARDED_FOR' ); else if (getenv('HTTP_CLIENT_ip')) $realip = getenv('HTTP_CLIENT_ip' ); else $realip = getenv('REMOTE_ADDR' );} return $realip;} function array2json($  arr) {if (function_exists('json_encode')) return json_encode($arr); $keys = array_keys($arr); $isarr = true; $json = ""; for($i=0;$i!i", "[img $1/]", $content); $cOntent= str_replace(array('<','>'),  array('<','>'), $content); $cOntent = preg_replace("!\[img (.*?)/\]!i", "", $content  ); $cOntent= str_replace($lang['ban'],'', $content); $cOntent= ($style)?"{$content}":$content;  $ubbarray = array('[:ani_wink:]','[:big_eyes:]','[:cool:]','[:cry:]','[:eye_roll:]','[:grin:  ]','[:happy:]','[:not_impressed:]','[:smile:]','[:smile_eyes:]','[:stickout:]','[:straight:]'  ,'[:su  rprised:]','[:unhappy:]','[:wink:]'); $cOntent = str_replace($ubbarray, array('','<  img src="smilies/big_eyes.gif" />','','','','','','','','','','','','',''), $content); $s.= $t."|".$name.":".$content."\  n";} if (!$name) die("No Name!!"); if (!$s) die("No Content!!"); $fp = @fopen($filename,"a+");  if (!$fp) die("repeat"); if (@flock($fp, LOCK_EX)) {@fputs($fp,$s); @flock($fp, LOCK_UN);} else die("repeat  "); @fclose($fp); echo "OK";} else if (trim($action) == "read") {if (get_magic_quotes_runtime()) {set_magic_quotes  _runtime(0);} $first = $_GET["first"]; $lastmod = intval($_GET["lastmod"]); $alastmod = @filemtime($filename); $name = file_get_contents("php:/  /input"); $name = str_replace("\n","",$name); $ip = get_ip(); $json = array(); $json["lastmod"] = $alastmod; $item =  array(); $newOnline = array(); $offline = array(); $lines = @file($filename); if ($alastmod> $lastmod && !$first) {foreach($lines as $l) {  $item2 = array(); $l = str_replace(array("\n","\r"),"",$l); if (strpos($l,"|") === false) continue;  $arr = explode("|",$l); $t = intval($arr[0]); if ($t> $lastmod) {$item2["time"] = date("H:i:s  ",$t); $item2["word"] = stripslashes($arr[1]); $item[] = $item2;}}} else if ($first) {$item = array(); $total  = count($lines); for($i=$total-1;$i>=$total-$least;$i--) {if ($i<=0) break; $item2 = array();  $l = str_replace(array("\n","\r"),"",$lines[$i]); if (strpos($l,"|") === false) continue; $arr =  explode("|",$l); $t = intval($arr[0]); $item2["time"] = (date("md",time()) == date("md",$  t))?  ;date("H:i:s",$t):date("md H:i",$t); $item2["word"] = stripslashes($arr[1]); $item[] =  $item2;} $item = array_reverse($item);} $s = ""; $nt = time(); $Onlines = array(); if($disonline) {$users = @file($datafile);  foreach($users as $l) {$l = str_replace(array("\r","\n"),"",$l); if (strpos($l,"|") === false)  {$s.=$l."\n"; continue;} $arr = explode("|",$l); if ($nt-intval($arr[1]) <$touchs*2+1)  {if (trim($name) == trim($arr[2])) {$s.= $arr[0]."|".time()."|".$name."|".get_ip  ()."|\n";} else $s.=$l."\n"; $onlines [] = $arr[2];}} @file_put_contents($datafile,$s); $json["  onlines"] = $onlines;} $json["lines"] = $item; echo array2json($json); if (!get_magic_quotes_runtime()) {set_magic_quotes_runtime(1);}} else {?> 

            

This article is published by www.internetweblist.com and does not represent the position of www.internetweblist.com/:http://www.internetweblist.com/programming/82402.html

Contact Us

Online consultation:click here to give a message