网站地图    收藏   

主页 > php专栏 > php综合实列 >

ajax php 聊天室实例代码(3) - 综合实例

来源:自学PHP网    时间:2014-12-02 13:09 作者: 阅读:次

[导读] if(!$s)die(nocontent!!);$fp=@fopen($filename,a+);if(!$fp)die(repeat);$re_time=0;while(!@flock($fp,lock_ex)){sleep(1);$re_tim...

ajax php 聊天室实例代码(3)

  1. if (!$s) die("no content!!"); 
  2.  $fp = @fopen($filename,"a+"); 
  3.  if (!$fp) die("repeat"); 
  4.  $re_time = 0; 
  5.  while(!@flock($fp, lock_ex)) 
  6.  { 
  7.   sleep(1); 
  8.   $re_time++; 
  9.   if ($re_time >=4) break; 
  10.  } 
  11.  if ($re_time <4) 
  12.  { 
  13.   @fputs($fp,$s); 
  14.   @flock($fp, lock_un); 
  15.  } 
  16.  else die("repeat"); 
  17.  @fclose($fp); 
  18.  echo "ok"; 
  19. } 
  20. else if ($action == "read") 
  21. { 
  22.  $first = $_post["first"]; 
  23.  $lastmod = intval($_post["lastmod"]) - $get_past_sec; //得到两秒以内的所有发言, 
  24.  $alastmod = @filemtime($filename); 
  25.  if ($lastmod - $alastmod > 360*48) die; 
  26.  $name = $_post['name']; 
  27.  $name = str_replace("n","",$name); 
  28.  $ip = get_ip(); 
  29.  $json = array(); 
  30.  $json["lastmod"] = time(); 
  31.  $item = array(); 
  32.  $newonline = array(); 
  33.  $offline = array(); 
  34.  $fp = @fopen($filename,'r'); 
  35.  flock($fp,lock_ex); 
  36.  $s = fread($fp,filesize($filename)); 
  37.  flock($fp,lock_un); 
  38.  fclose($fp); 
  39.  $lines = explode("n",$s); 
  40.   
  41.  if ($alastmod >= $lastmod && !$first) 
  42.  { 
  43.   foreach($lines as $l) 
  44.   { 
  45.    $item2 = array(); 
  46.    $l = str_replace(array("n","r"),"",$l); 
  47.    if (strpos($l,"|") === false) continue; 
  48.    $arr = explode("|",$l); 
  49.    $t = intval($arr[0]); 
  50.    if ($t >= $lastmod) 
  51.    { 
  52.     $item2["time"] = date("h:i:s",$t); 
  53.     $item2["word"] = addslashes($arr[1]); 
  54.     $item[] = $item2; 
  55.    } 
  56.   } 
  57.  } 
  58.  else if ($first) 
  59.  { 
  60.   $item = array(); 
  61.   $total = count($lines); 
  62.   for($i=$total-1;$i>=$total-$least;$i--) 
  63.   { 
  64.    if ($i<=0) break; 
  65.    $item2 = array(); 
  66.    $l = str_replace(array("n","r"),"",$lines[$i]); 
  67.    if (strpos($l,"|") === false) continue; 
  68.    $arr = explode("|",$l); 
  69.    $t = intval($arr[0]); 
  70.    $item2["time"] = (date("m-d",time()) == date("m-d",$t))?date("h:i:s",$t):date("m-d h:i",$t); 
  71.    $item2["word"] = addslashes($arr[1]); 
  72.    $item[] = $item2; 
  73.   } 
  74.   $item = array_reverse($item); 
  75.  } 
  76.   
  77.  $s = ""; 
  78.  $nt = time(); 
  79.  $onlines = array(); 
  80.  if($disonline && $touchme) 
  81.  { 
  82.   $users = @file($datafile); 
  83.   foreach($users as $l) 
  84.   { 
  85.    $l = str_replace(array("r","n"),"",$l); 
  86.    if (strpos($l,"|") === false) 
  87.    { 
  88.     $s.=$l."n"; 
  89.     continue; 
  90.    } 
  91.    $arr = explode("|",$l); 
  92.    if ($nt - intval($arr[1]) < $touchs*3) 
  93.    { 
  94.     if (trim($name) == trim($arr[2])) 
  95.     { 
  96.      $s.= $arr[0]."|".time()."|".$name."|".get_ip()."|n"; 
  97.     } 
  98.     else $s.=$l."n"; 
  99.     $onlines [] = htmlspecialchars($arr[2]); 
  100.    } 
  101.   } 
  102.   @file_put_contents($datafile,$s); 
  103.   $json["onlines"] = $onlines; 
  104.  } 
  105.  $json["lines"] = $item; 
  106.  echo array2json($json); 
  107. } 
  108. else if ($action == "keep" ) 
  109. { 
  110.  keeponline(); 
  111.  echo "keep ok"; 
  112. } 
  113. else if ($action == "quit") 
  114. { 
  115.  $name = $_post['name']; 
  116.  if($disonline) 
  117.  { 
  118.   $users = @file($datafile); 
  119.   foreach($users as $l) 
  120.   { 
  121.    $l = str_replace(array("r","n"),"",$l); 
  122.    if (strpos($l,"|") === false) 
  123.    { 
  124.     $s.=$l."n"; 
  125.     continue; 
  126.    } 
  127.    $arr = explode("|",$l); 
  128.    if (trim($name) == trim($arr[2])) continue; 
  129.    else $s.=$l."n"; 
  130.   } 
  131.   @file_put_contents($datafile,$s); 
  132.   echo "ok"; 
  133.  } 
  134.  die(); 
  135. } 
  136. else 
  137. { 
  138. ?>

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论