ajax php 聊天室实例代码(2)
发布:smiling 来源: PHP粉丝网 添加日期:2013-12-16 16:18:03 浏览: 评论:0
- 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)
- {
- $keys = array_keys($arr);
- $isarr = true;
- $json = "";
- for($i=0;$i<count($keys);$i++)
- {
- if ($keys[$i] !== $i)
- {
- $isarr = false;
- break;
- }
- }
- $json = $space;
- $json.= ($isarr)?"[":"{";
- for($i=0;$i<count($keys);$i++)
- {
- if ($i!=0) $json.= ",";
- $item = $arr[$keys[$i]];
- $json.=($isarr)?"":$keys[$i].':';
- if (is_array($item))
- $json.=array2json($item);
- else if (is_string($item))
- $json.='"'.str_replace(array("r","n"),"",$item).'"';
- else $json.=$item;
- }
- $json.= ($isarr)?"]":"}";
- return $json;
- }
- function keeponline()
- {
- global $disonline,$datafile;
- if (!$disonline) return;
- $name = $_post['name'];
- $ip = get_ip();
- $onlines = @file_get_contents($datafile);
- $s1 = "|{$name}|{$ip}|";
- if (strpos($onlines,$s1) === false)
- {
- if (strpos($onlines,"|".$name."|") === false)
- {
- $fp = @fopen($datafile,"a+");
- if ($fp)
- {
- if (@flock($fp, lock_ex))
- {
- @fputs($fp,time()."|".time().$s1."n");
- @flock($fp, lock_un);
- }
- @fclose($fp);
- }
- }
- else
- {
- echo "name";
- die();
- }
- }
- }
- if ($action == "write")
- {
- $color = $_post["color"];
- if (!eregi("[0-9a-fa-f]{6}",$color) || $color == "#000000") $color = "";
- $color = "#".$color;
- $size = intval($_post["size"]);
- $name = htmlspecialchars(str_replace(array("n","r"),"",$_post['name']));
- if (!$name) die("no name!!");
- $ip = get_ip();
- keeponline();
- $s = "";
- $style = "";
- $font = $_post["font"];
- if ($font == "songti") $font = "宋体";
- else if ($font == "heiti") $font = "黑体";
- else if ($font == "kaiti") $font = "楷体_gb2312";
- else $font = "";
- $style .= (!$font)?"":"font-family:".$font.";";
- $style .= (!$_post["bold"])?"":"font-weight:bold;";
- $style .= (!$color || $color == "#")?"":"color:{$color};";
- $style .= (!$size || $size == "16")?"":"font-size:{$size}px;";
- $t = time();
- $arr = explode("n",$_post['content']);
- if (count($arr) > 20) die('error');
- for($i = 0;$i<count($arr);$i++)
- {
- $content = $arr[$i];
- $content = trim($content);
- $content = str_replace(array("n","r"),"",$content);
- if (!$content) continue;
- $content = htmlspecialchars($content);
- $content = preg_replace("~[img](http://[a-za-z0-9.-_+%?]*)[/img]~i", "<img src='$1' />", $content);
- $content = ($style)?"<span style='{$style}'>{$content}</span>":$content;
- $s.= $t."|".$name.":".$content."n";
- }
Tags: ajax 聊天室 实例
相关文章
- ·ajax php 聊天室实例代码(1)(2013-12-16)
- ·ajax php 聊天室实例代码(3)(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)