php網站怎麼寫一個聊天

2020-07-16 10:06:46

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>

結果展示

1.jpg

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網站怎麼寫一個聊天的詳細內容,更多請關注TW511.COM其它相關文章!