类别:PHP问题 / 日期:2019-12-02 / 浏览:75 / 评论:0

php网站怎样写一个谈天

网页谈天室我们能够运用多种体式格局完成,比方websocket,或是运用第三方的谈天效劳,下面引见一种最简朴的体式格局,不停革新页面猎取信息。

引荐进修:PHP视频教程

1、数据库竖立

create table chat (
  chattime datetime,
  nick char(10),
  words char(150)
);

login.php

<html>
<head>
  <title>用户登录</title>
  <meta charset="utf-8">
</head>
<body>请输入您的昵称<br>
<form action="main.php" method="post" target="_self">  //点击登录后跳转到main.php,并将输入的数据用post的体式格局发送过去
  <input type="text" name="nick" cols="20">
  <input type="submit" value="登录">
</body>
</html>

main.php

<?php
  session_start();
  $_SESSION['nick'] = $_POST['nick']; //猎取login.php发送过来的数据,也就是用户昵称,并将它保存在session中用于对用户举行跟踪
?>
<html>
  <frameset rows="80%, 20%">
  <frame src="cdisplay.php" name="chatdisplay"/>   // 谈天信息展现区
  <frame src="speak.php" name="speak"/>    //谈话区
  </frameset>
</html>

speak.php

<html>
<head>
  <title>谈话</title>
  <meta charset="utf-8">
</head>
<body>
<?php
   session_start(); //假如设置北京时候,须要加上  date_default_timezone_set('PRC');
   if ($_POST['words']) {
   $conn = mysql_connect("127.0.0.1","root","******");  //衔接数据库
   mysql_select_db("yuema", $conn);
   $time = date(y).date(m).date(d).date(h).date(i).date(s);  //当前时候
   $nick = $_SESSION['nick'];
   $words = $_POST['words'];
   $str = "insert into chat(chattime, nick, words) values('$time', '$nick', '$words');"; 
   mysql_query($str, $conn);  //将用户名,时候和谈话内容举行插进去
   mysql_close($conn);
}
?>
 
<form action="speak.php" method="post" target="_self">
  <input type="text" name="words" cols="20">
  <input type="submit" value="谈话">
</form>
</body>
</html>

cdisplay.php

<html>
<head>
  <title>显现用户谈话</title>
  <meta http-equiv="refresh" content="5;url=cdisplay.php">  //设置每隔5秒钟革新一次
</head>
<body>
<?php
  $conn = mysql_connect("127.0.0.1", "root", "******");
  mysql_select_db("yuema", $conn);
  $str = "select * from chat order by chattime;";
  $result = mysql_query($str, $conn);
  $rows = mysql_num_rows($result);
  mysql_data_seek($result, $rows-15); //取近来插进去的15条数据
  if ($rows<15)
    $l = $rows;
  else  
    $l = 15;
  for ($i = 1; $i <= $l; $i++) {    //输出这15条数据
    list($chattime, $nick, $words) = mysql_fetch_row($result);
    echo $chattime;
    echo " ".$nick." ";
    echo $words;
    echo "<br>";
  }
?>
</body>
</html>

效果展现

2. ajax猎取,不革新页面

login.php

<html>
<head>
  <title>用户登录</title>
  <meta charset="utf-8">
</head>
<body>请输入您的昵称<br>
<form action="main.php" method="post" target="_self">  //点击登录后跳转到main.php,并将输入的数据用post的体式格局发送过去
  <input type="text" name="nick" cols="20">
  <input type="submit" value="登录">
</body>
</html>

main.php

<?php
  session_start();
  $_SESSION['nick'] = $_POST['nick']; //猎取login.php发送过来的数据,也就是用户昵称,并将它保存在session中用于对用户举行跟踪
?>
<html>
  <frameset rows="80%, 20%">
  <frame src="cdisplay.php" name="chatdisplay"/>   // 谈天信息展现区
  <frame src="speak.php" name="speak"/>    //谈话区
  </frameset>
</html>

speak.php

<html>
<head>
  <title>谈话</title>
  <meta charset="utf-8">
</head>
<body>
<?php
   session_start();   //假如设置北京时候,须要加上  date_default_timezone_set('PRC');
   if ($_POST['words']) {
   $conn = mysql_connect("127.0.0.1","root","******");  //衔接数据库
   mysql_select_db("yuema", $conn);
   $time = date(y).date(m).date(d).date(h).date(i).date(s);  //当前时候
   $nick = $_SESSION['nick'];
   $words = $_POST['words'];
   $str = "insert into chat(chattime, nick, words) values('$time', '$nick', '$words');"; 
   mysql_query($str, $conn);  //将用户名,时候和谈话内容举行插进去
   mysql_close($conn);
}
?>
 
<form action="speak.php" method="post" target="_self">
  <input type="text" name="words" cols="20">
  <input type="submit" value="谈话">
</form>
</body>
</html>

cdisplay.php

<html>
<head>
  <meta charset="utf-8">
  <title>显现用户谈话</title>
  <script type="text/javascript" src="jquery.js"></script>  //jquery库,jquery.js能够在网上下载
  <script type="text/javascript">
      setInterval('show()', 3000);   // 设置自动革新时候 3000毫秒也就是3秒钟
         function show() {
         $.ajax({
            url:'server_get.php',  //要求发送到server_get.php举行处置惩罚
            type:'post',
            dataType:'html',
            error:function() {
              alert('要求失利,请稍后再试');
            },
            success:function(msg) {
            $('p').html(msg);  //设置body中p标签的内容
            }
       });
    }
  </script>
</head>
<body>
<p></p>
</body>
</html>

server_get.php

<?php
  $conn = mysql_connect("127.0.0.1", "root", "******");
  mysql_select_db("yuema", $conn);
  $str = "select * from chat order by chattime;";
  $result = mysql_query($str, $conn);
  $rows = mysql_num_rows($result);
  mysql_data_seek($result, $rows-15);
  if ($rows < 15) 
    $l = $rows;
  else
    $l = 15; 
  $string = ""; 
  for ($i = 1; $i <= $l; $i++) {
    list($chattime, $nick, $words) = mysql_fetch_row($result);
    $string.=$chattime;
    $string.=" ";
    $string.=$nick;
    $string.=" ";
    $string.=$words;
    $string.="<br>";
  }
  echo $string; 
?>

以上就是php网站怎样写一个谈天的细致内容,更多请关注ki4网别的相干文章!

打赏

感谢您的赞助~

打开支付宝扫一扫,即可进行扫码打赏哦~

版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。