php網(wǎng)站如何寫一個(gè)聊天

這篇文章將為大家詳細(xì)講解有關(guān)php網(wǎng)站如何寫一個(gè)聊天,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站主營(yíng)沅江網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app開發(fā),沅江h(huán)5成都微信小程序搭建,沅江網(wǎng)站營(yíng)銷推廣歡迎沅江等地區(qū)企業(yè)咨詢

php網(wǎng)站怎么寫一個(gè)聊天

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

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

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

login.php

<html>
<head>
  <title>用戶登錄</title>
  <meta charset="utf-8">
</head>
<body>請(qǐng)輸入您的昵稱<br>
<form action="main.php" method="post" target="_self">  //點(diǎn)擊登錄后跳轉(zhuǎn)到main.php,并將輸入的數(shù)據(jù)用post的方式發(fā)送過(guò)去
  <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ā)送過(guò)來(lái)的數(shù)據(jù),也就是用戶昵稱,并將它保存在session中用于對(duì)用戶進(jìn)行跟蹤
?>
<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(); //如果設(shè)置北京時(shí)間,需要加上  date_default_timezone_set('PRC');
   if ($_POST['words']) {
   $conn = MySQL_connect("127.0.0.1","root","******");  //連接數(shù)據(jù)庫(kù)
   mysql_select_db("yuema", $conn);
   $time = date(y).date(m).date(d).date(h).date(i).date(s);  //當(dāng)前時(shí)間
   $nick = $_SESSION['nick'];
   $words = $_POST['words'];
   $str = "insert into chat(chattime, nick, words) values('$time', '$nick', '$words');"; 
   mysql_query($str, $conn);  //將用戶名,時(shí)間和發(fā)言內(nèi)容進(jìn)行插入
   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">  //設(shè)置每隔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>

結(jié)果展示

php網(wǎng)站如何寫一個(gè)聊天

2. ajax獲取,不刷新頁(yè)面

login.php

<html>
<head>
  <title>用戶登錄</title>
  <meta charset="utf-8">
</head>
<body>請(qǐng)輸入您的昵稱<br>
<form action="main.php" method="post" target="_self">  //點(diǎn)擊登錄后跳轉(zhuǎn)到main.php,并將輸入的數(shù)據(jù)用post的方式發(fā)送過(guò)去
  <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ā)送過(guò)來(lái)的數(shù)據(jù),也就是用戶昵稱,并將它保存在session中用于對(duì)用戶進(jìn)行跟蹤
?>
<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();   //如果設(shè)置北京時(shí)間,需要加上  date_default_timezone_set('PRC');
   if ($_POST['words']) {
   $conn = mysql_connect("127.0.0.1","root","******");  //連接數(shù)據(jù)庫(kù)
   mysql_select_db("yuema", $conn);
   $time = date(y).date(m).date(d).date(h).date(i).date(s);  //當(dāng)前時(shí)間
   $nick = $_SESSION['nick'];
   $words = $_POST['words'];
   $str = "insert into chat(chattime, nick, words) values('$time', '$nick', '$words');"; 
   mysql_query($str, $conn);  //將用戶名,時(shí)間和發(fā)言內(nèi)容進(jìn)行插入
   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庫(kù),jquery.js可以在網(wǎng)上下載
  <script type="text/javascript">
      setInterval('show()', 3000);   // 設(shè)置自動(dòng)刷新時(shí)間 3000毫秒也就是3秒鐘
         function show() {
         $.ajax({
            url:'server_get.php',  //請(qǐng)求發(fā)送到server_get.php進(jìn)行處理
            type:'post',
            dataType:'html',
            error:function() {
              alert('請(qǐng)求失敗,請(qǐng)稍后再試');
            },
            success:function(msg) {
            $('p').html(msg);  //設(shè)置body中p標(biāo)簽的內(nèi)容
            }
       });
    }
  </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; 
?>

關(guān)于php網(wǎng)站如何寫一個(gè)聊天就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:php網(wǎng)站如何寫一個(gè)聊天
文章起源:http://bm7419.com/article46/pcoeeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站維護(hù)、域名注冊(cè)、網(wǎng)站營(yíng)銷、軟件開發(fā)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)