ajax php 聊天室实例代码(3)
发布:smiling 来源: PHP粉丝网 添加日期:2013-12-16 16:19:10 浏览: 评论:0
- if (!$s) die("no content!!");
- $fp = @fopen($filename,"a+");
- if (!$fp) die("repeat");
- $re_time = 0;
- while(!@flock($fp, lock_ex))
- {
- sleep(1);
- $re_time++;
- if ($re_time >=4) break;
- }
- if ($re_time <4)
- {
- @fputs($fp,$s);
- @flock($fp, lock_un);
- }
- else die("repeat");
- @fclose($fp);
- echo "ok";
- }
- else if ($action == "read")
- {
- $first = $_post["first"];
- $lastmod = intval($_post["lastmod"]) - $get_past_sec; //得到两秒以内的所有发言,
- $alastmod = @filemtime($filename);
- if ($lastmod - $alastmod > 360*48) die;
- $name = $_post['name'];
- $name = str_replace("n","",$name);
- $ip = get_ip();
- $json = array();
- $json["lastmod"] = time();
- $item = array();
- $newonline = array();
- $offline = array();
- $fp = @fopen($filename,'r');
- flock($fp,lock_ex);
- $s = fread($fp,filesize($filename));
- flock($fp,lock_un);
- fclose($fp);
- $lines = explode("n",$s);
- 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"] = addslashes($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("m-d",time()) == date("m-d",$t))?date("h:i:s",$t):date("m-d h:i",$t);
- $item2["word"] = addslashes($arr[1]);
- $item[] = $item2;
- }
- $item = array_reverse($item);
- }
- $s = "";
- $nt = time();
- $onlines = array();
- if($disonline && $touchme)
- {
- $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*3)
- {
- if (trim($name) == trim($arr[2]))
- {
- $s.= $arr[0]."|".time()."|".$name."|".get_ip()."|n";
- }
- else $s.=$l."n";
- $onlines [] = htmlspecialchars($arr[2]);
- }
- }
- @file_put_contents($datafile,$s);
- $json["onlines"] = $onlines;
- }
- $json["lines"] = $item;
- echo array2json($json);
- }
- else if ($action == "keep" )
- {
- keeponline();
- echo "keep ok";
- }
- else if ($action == "quit")
- {
- $name = $_post['name'];
- 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 (trim($name) == trim($arr[2])) continue;
- else $s.=$l."n";
- }
- @file_put_contents($datafile,$s);
- echo "ok";
- }
- die();
- }
- else
- {
- ?>
Tags: ajax 聊天室 实例
相关文章
- ·ajax php 聊天室实例代码(1)(2013-12-16)
- ·ajax php 聊天室实例代码(2)(2013-12-16)
- ·ajax php 聊天室实例代码(4)(2013-12-16)
- ·ajax php 聊天室实例代码(5)(2013-12-16)
- ·PHP ajax 统计当前在线用户数程序代码(2014-01-06)
- ·php ajax用户登录代码(2014-01-15)
- ·php ajax用户登陆验证程序代码(2014-01-16)
- ·php+ajax实现无刷新的新闻留言系统(2014-02-10)
- ·php jquery ajax用户登录实例代码(2014-07-04)
- ·php+jquery ajax邮箱地址无刷新验证实例(2014-07-04)
- ·ajax+php无刷新回贴和注册检验实例(2014-07-05)
- ·php ajax 留言板(2014-09-09)
- ·php+ajax注册验证实例(完整代码)(2015-04-10)
- ·PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程(2021-04-09)
- ·PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程(2021-04-09)
- ·PHP+jQuery+Ajax实现用户登录与退出(2021-05-25)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)