php聊天功能_php怎么实现聊天功能

638676776bae7714c026c13c85a785b7.png

php怎么实现聊天功能

1、在前端使用Ajax定时轮询访问php脚本,php再进行查询聊天数据进行返回给前端,最后前端将其进行渲染;

代码示例:

聊天室

$(function(){

$("#post").click(function(){

var content=$("#content").val();

if(!$.trim(content)){

alert('请填写内容');

return false;

}

$("#content").val("");

$.post("ajax.php", {content:content});});

})

function getData(msg){

if(msg == undefined)

{

msg = '';

}

$.post("get.php",{"msg":msg},function(data){

//var myjson = eval("("+data+")");

if(data){

var chatcontent = '';

var obj = eval('('+data+')');

$.each(obj,function(key,val){

chatcontent += "

"+val['username']+" 说:
";

chatcontent += "

"+val['content']+"
";

})

$("#chatshow").html(chatcontent);

}

getData();

})

}

// function getUser(msg){

// if(msg == undefined){

// msg = '';

// }

// $.post("getuser.php",{"msg":msg},function(data){

// //var myjson = eval("("+data+")");

// if(data){

// var chatcontent = '';

// var obj = eval('('+data+')');

// $.each(obj,function(key,val){

// chatcontent += "

"+val['username']+" 说:
";

// chatcontent += "

"+val['content']+"
";

// })

// $("#chatshow").html(chatcontent);

// }

// getData();

// })

// }

getData("one");

// $(function(){

// $("#userlist p").click(function(){

// $("#content").val("@"+$(this).text()+" ");

// })

// })

// $(function(){

// $(document).keypress(function(e){

// if(e.ctrlKey && e.which == 13 || e.which == 10) {

// var content=$("#content").val();

// if(!$.trim(content)){

// alert('请填写内容');

// return false;

// }

// $("#content").val("");

// $.post("ajax.php", {content:content});

// }

// })

// //alert(event.clientX+document.body.clientWidth);

// })

/*window.οnbefοreunlοad=function (){

//return("===οnbefοreunlοad===");

if(event.clientX>document.body.clientWidth && event.clientY < 0 || event.altKey){

return("你关闭了浏览器");

}else{

return(document.body.clientWidth);

}

} */

#chat{margin:0 auto;}

#chatshow{width:500px;height:400px;overflow:auto;border:1px solid #ccc;float:left;}

#userlist{width:100px;height:400px;overflow:auto;border:1px solid #ccc;float:left;margin-right:2px;}

#userlist p{color:#0F0; cursor:pointer;}

.clearboth{clear:both;}

.username{font-weight:bold;color:#00F;font-size:12px;margin-bottom:3px;margin-top:8px;}

在线用户列表

// $dsn = "mysql:host=localhost;dbname=test;charset=utf8'";

// $db = new PDO($dsn, 'root', 'root');

$db = new PDO('mysql:dbname=test;host=127.0.0.1;charset=utf8', 'root', 'adminroot');

$rs = $db->prepare("select * from chat where is_login = '1'");

$rs->execute();

while($row = $rs->fetch()){

echo '

'.$row['username'].'

';

}

?>

/*session_start();

$mem = new Memcache;

$mem->connect("localhost",11211);

if(isset($_POST['content'])){

$con['u

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值