网站地图    收藏   

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

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

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

[导读] functionget_ip(){global$_server;if($_server){if($_server[http_x_forwarded_for])$realip=$_server[http_x_forwarded_for];elseif($_s...

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

  1. function get_ip() 
  2. { 
  3.  global $_server; 
  4.  if ($_server) 
  5.  { 
  6.   if ( $_server[http_x_forwarded_for] ) 
  7.    $realip = $_server["http_x_forwarded_for"]; 
  8.   else if ( $_server["http_client_ip"] ) 
  9.    $realip = $_server["http_client_ip"]; 
  10.   else 
  11.    $realip = $_server["remote_addr"]; 
  12.  } 
  13.  else 
  14.  { 
  15.   if ( getenv( 'http_x_forwarded_for' ) ) 
  16.    $realip = getenv( 'http_x_forwarded_for' ); 
  17.   else if ( getenv( 'http_client_ip' ) )  
  18.    $realip = getenv( 'http_client_ip' ); 
  19.   else 
  20.    $realip = getenv( 'remote_addr' ); 
  21.  } 
  22.  return $realip; 
  23. } 
  24. function array2json($arr) 
  25. { 
  26.  $keys = array_keys($arr); 
  27.  $isarr = true; 
  28.  $json = ""; 
  29.  for($i=0;$i<count($keys);$i++) 
  30.  { 
  31.   if ($keys[$i] !== $i) 
  32.   { 
  33.    $isarr = false; 
  34.    break; 
  35.   } 
  36.  } 
  37.  $json = $space; 
  38.  $json.= ($isarr)?"[":"{"; 
  39.  for($i=0;$i<count($keys);$i++) 
  40.  { 
  41.   if ($i!=0) $json.= ","; 
  42.   $item = $arr[$keys[$i]]; 
  43.   $json.=($isarr)?"":$keys[$i].':'; 
  44.   if (is_array($item)) 
  45.    $json.=array2json($item); 
  46.   else if (is_string($item)) 
  47.    $json.='"'.str_replace(array("r","n"),"",$item).'"'; 
  48.   else $json.=$item; 
  49.  } 
  50.  $json.= ($isarr)?"]":"}"; 
  51.  return $json; 
  52. } 
  53. function keeponline() 
  54. { 
  55.  global $disonline,$datafile; 
  56.  if (!$disonline) return; 
  57.  $name = $_post['name']; 
  58.  $ip = get_ip(); 
  59.  $onlines = @file_get_contents($datafile); 
  60.  $s1 = "|{$name}|{$ip}|"; 
  61.  if (strpos($onlines,$s1) === false) 
  62.  { 
  63.   if (strpos($onlines,"|".$name."|") === false) 
  64.   { 
  65.    $fp = @fopen($datafile,"a+"); 
  66.    if ($fp) 
  67.    { 
  68.     if (@flock($fp, lock_ex)) 
  69.     { 
  70.      @fputs($fp,time()."|".time().$s1."n"); 
  71.      @flock($fp, lock_un); 
  72.     } 
  73.     @fclose($fp); 
  74.    } 
  75.   } 
  76.   else 
  77.   { 
  78.    echo "name"; 
  79.    die(); 
  80.   } 
  81.  } 
  82. } 
  83. if ($action == "write") 
  84. { 
  85.  $color = $_post["color"]; 
  86.  if (!eregi("[0-9a-fa-f]{6}",$color) || $color == "#000000") $color = ""; 
  87.  $color = "#".$color; 
  88.  $size = intval($_post["size"]); 
  89.  $name = htmlspecialchars(str_replace(array("n","r"),"",$_post['name'])); 
  90.  if (!$name) die("no name!!"); 
  91.  $ip = get_ip(); 
  92.  keeponline(); 
  93.   
  94.  $s = ""; 
  95.  $style = ""; 
  96.  $font = $_post["font"]; 
  97.  if ($font == "songti") $font = "宋体"; 
  98.  else if ($font == "heiti") $font = "黑体"; 
  99.  else if ($font == "kaiti") $font = "楷体_gb2312"; 
  100.  else $font = ""; 
  101.  $style .= (!$font)?"":"font-family:".$font.";"; 
  102.  $style .= (!$_post["bold"])?"":"font-weight:bold;"; 
  103.  $style .= (!$color || $color == "#")?"":"color:{$color};"; 
  104.  $style .= (!$size || $size == "16")?"":"font-size:{$size}px;"; 
  105.  $t = time(); 
  106.  $arr = explode("n",$_post['content']); 
  107.  if (count($arr) > 20) die('error'); 
  108.  for($i = 0;$i<count($arr);$i++) 
  109.  { 
  110.   $content = $arr[$i]; 
  111.   $content = trim($content); 
  112.   $content = str_replace(array("n","r"),"",$content); 
  113.   if (!$content) continue; 
  114.   $content = htmlspecialchars($content); 
  115.   $content = preg_replace("~[img](http://[a-za-z0-9.-_+%?]*)[/img]~i", "<img src='$1' />", $content); 
  116.   $content = ($style)?"<span style='{$style}'>{$content}</span>":$content; 
  117.   $s.= $t."|".$name.":".$content."n"; 
  118.  } 

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

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

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

添加评论