找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 204|回复: 0
打印 上一主题 下一主题

值得分享的php+ajax实时聊天室

[复制链接]

2647

主题

2647

帖子

7881

积分

论坛元老

Rank: 8Rank: 8

积分
7881
跳转到指定楼层
楼主
发表于 2018-2-14 05:34:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

            非常经典的一款php+ajax实时聊天室,其中使用PHP文件保存聊天记录,按天划分,PHP实现聊天的功能只有一个文件,整合了PHP与AJAX技术,也就是说只要运行这一个文件就可以启动PHP的聊天室了,关于代码上面也是非常的简单,但是实现了聊天室一般的功能,聊天时的昵称,更改昵称的颜色,聊天字号大小,字体,加粗,窗体的变大变小等等,如果你想搞个聊天室来玩玩,这个源码完全可以满足普通的需求。
具体的效果看如下图:

关键代码:
"聊天室.",
//聊天室标题
"title"=>"Welcome...!",
//第一个到聊天室的欢迎
"firstone"=>"Welcome...!",
//当信息有禁止内容时显示
"ban" => array('法轮功', '共产党', '李洪志', 'fuck', '叼', '你妈的', '他妈的'),
//关键字
"keywords"=>"Welcome...!",
//发言提示
"hereyourwords" => "在这里发言!"
);
$touchs = 10;
$title = $lang["title"];
$earlier = 10;
$description = $lang["description"];
$origroom = $room;
$least = ($_GET["dis"])?intval($_GET["dis"])leastnum;
if ($_GET["room"]) $room = $_GET["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,''."\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'), array(''), $content);
$content = preg_replace("!\[(.*?)/\]!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:]',
'[:surprised:]',
'[:unhappy:]',
'[:wink:]');
$content = str_replace($ubbarray,
array('

',
'

',
'

',
'

',
'

',
'

',
'

',
'

',
'

',
'

',
'

',
'

',
'

',
'

',
'

'),
$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
安装说明:
因为这一款php+ajax实时聊天室的聊天记录是保存到PHP文件中的,所以不用导入数据库,安装自然也就方便多了,只需要将下载的文件包解压缩到可以运行PHP的根目录下即可.
源码下载:php+ajax实时聊天室
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
            
            
您可能感兴趣的文章:
  • ASP.NET 使用application与session对象写的简单聊天室程序
  • 使用Meteor配合Node.js编写实时聊天应用的范例
  • ASP.NET网站聊天室的设计与实现(第3节)
  • Asp.net使用SignalR实现酷炫端对端聊天功能
  • Asp.net使用SignalR实现聊天室的功能
  • javascript和jQuery实现网页实时聊天的ajax长轮询
  • asp.net mvc signalr简单聊天室制作过程分析
  • HTML5基于Tomcat 7.0实现WebSocket连接并实现简单的实时聊天
  • Redis实现多人多聊天室功能
  • Asp.net SignalR创建实时聊天应用程序
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端