溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php網站如何寫一個聊天

發(fā)布時間:2020-09-16 10:21:52 來源:億速云 閱讀:142 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關php網站如何寫一個聊天,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

php網站怎么寫一個聊天

網頁聊天室我們可以使用多種方式實現(xiàn),比如websocket,或是使用第三方的聊天服務,下面介紹一種最簡單的方式,不斷刷新頁面獲取信息。

1、數(shù)據(jù)庫建立

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,并將輸入的數(shù)據(jù)用post的方式發(fā)送過去
  <input type="text" name="nick" cols="20">
  <input type="submit" value="登錄">
</body>
</html>

main.php

<?php
  session_start();
  $_SESSION['nick'] = $_POST['nick']; //獲取login.php發(fā)送過來的數(shù)據(jù),也就是用戶昵稱,并將它保存在session中用于對用戶進行跟蹤
?>
<html>
  <frameset rows="80%, 20%">
  <frame src="cdisplay.php" name="chatdisplay"/>   // 聊天信息展示區(qū)
  <frame src="speak.php" name="speak"/>    //發(fā)言區(qū)
  </frameset>
</html>

speak.php

<html>
<head>
  <title>發(fā)言</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","******");  //連接數(shù)據(jù)庫
   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);  //將用戶名,時間和發(fā)言內容進行插入
   mysql_close($conn);
}
?>
 
<form action="speak.php" method="post" target="_self">
  <input type="text" name="words" cols="20">
  <input type="submit" value="發(fā)言">
</form>
</body>
</html>

cdisplay.php

<html>
<head>
  <title>顯示用戶發(fā)言</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條數(shù)據(jù)
  if ($rows<15)
    $l = $rows;
  else  
    $l = 15;
  for ($i = 1; $i <= $l; $i++) {    //輸出這15條數(shù)據(jù)
    list($chattime, $nick, $words) = mysql_fetch_row($result);
    echo $chattime;
    echo " ".$nick." ";
    echo $words;
    echo "<br>";
  }
?>
</body>
</html>

結果展示

php網站如何寫一個聊天

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,并將輸入的數(shù)據(jù)用post的方式發(fā)送過去
  <input type="text" name="nick" cols="20">
  <input type="submit" value="登錄">
</body>
</html>

main.php

<?php
  session_start();
  $_SESSION['nick'] = $_POST['nick']; //獲取login.php發(fā)送過來的數(shù)據(jù),也就是用戶昵稱,并將它保存在session中用于對用戶進行跟蹤
?>
<html>
  <frameset rows="80%, 20%">
  <frame src="cdisplay.php" name="chatdisplay"/>   // 聊天信息展示區(qū)
  <frame src="speak.php" name="speak"/>    //發(fā)言區(qū)
  </frameset>
</html>

speak.php

<html>
<head>
  <title>發(fā)言</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","******");  //連接數(shù)據(jù)庫
   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);  //將用戶名,時間和發(fā)言內容進行插入
   mysql_close($conn);
}
?>
 
<form action="speak.php" method="post" target="_self">
  <input type="text" name="words" cols="20">
  <input type="submit" value="發(fā)言">
</form>
</body>
</html>

cdisplay.php

<html>
<head>
  <meta charset="utf-8">
  <title>顯示用戶發(fā)言</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',  //請求發(fā)送到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網站如何寫一個聊天就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

php
AI