php網頁版聊天軟體實現代碼_php執行個體

來源:互聯網
上載者:User

本文執行個體為大家分享了php匿名聊天室的具體實現代碼,供大家參考,具體內容如下

1. index.html   

<html> <head>  <title>聊天室</title>  <meta charset="utf-8"/>  <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>  <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>  <link href="./css/style.css" rel="stylesheet"/>  <script>   var maxid = 0;   function showmessage() {    //建立ajax對象    var xhr = new XMLHttpRequest();    //監聽    xhr.onreadystatechange = function () {     if (xhr.readyState === 4) {      eval("var info=" + xhr.responseText);      var text = "";      for (var i = 0; i < info.length; i++)      {       text += "<div class='alert alert-success'>" +         "<span class='name'>" + info[i].send + ":</span>" +         "<span class='message'>" + info[i].content + "</span>" +         "<span>(" + info[i].time + ")</span>" +         "</div>";       maxid = info[i].id;      }       var old = document.getElementById("msg").innerHTML;      document.getElementById("msg").innerHTML = old + text;      document.getElementById("msg").scrollTop = document.getElementById("msg").scrollHeight;     }    };    //初始化    xhr.open("get", "./action.php?maxid=" + maxid);    //發送    xhr.send();   }    $(document).ready(     function ()     {      showmessage()      self.setInterval("showmessage()", 2000);     }   );   function send() {    var postData ="content="+document.getElementById('content').value;    var xhr = new XMLHttpRequest();    xhr.open("POST", "./add.php", true);    //一定要寫頭資訊不然伺服器接收不到    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    xhr.onreadystatechange = function () {     var XMLHttpReq = xhr;     if (XMLHttpReq.readyState == 4) {      if (XMLHttpReq.status == 200) {       document.getElementById('content').value="";      }     }    };    xhr.send(postData);   }  </script> </head> <body>   <div class="panel panel-default" id="main">   <!--聊天室名字-->    <div class="panel-heading">    <h3 class="panel-title">     匿名者聊天室    </h3>   </div>   <!--聊天室名字-->   <!--聊天室訊息框-->   <div class="panel-body ">    <div class="well no-bottom ">     <!--     訊息框樣式-->     <!--     <div class="alert alert-success">成功!很好地完成了提交。</div>     <div class="alert alert-info">資訊!請注意這個資訊。</div>     <div class="alert alert-warning">警告!請不要提交。</div>     <div class="alert alert-danger">錯誤!請進行一些更改。</div>     -->     <div id="msg" class="showmessage">     </div>     <!--聊天室訊息框-->     <div class="well" >      <form role="form">       <div class="form-group">        <label for="name">發送訊息</label>        <textarea class="form-control" id="content" name="content"           style="resize: none;font-family: Microsoft YaHei;" rows="3" >        </textarea>       </div>       <div style="text-align: right">        <button type="button" class="btn btn-primary" onclick="send()">         <span class="glyphicon glyphicon-envelope"></span>         發送        </button>       </div>      </form>     </div>     <!--聊天室發送框-->    </div>   </div>  </div> </body></html>

2. action.php

<?php $link = mysqli_connect('localhost', 'root', '123', 'test');mysqli_query($link, 'set names utf8');$info= array();header("Content-type: text/html; charset=utf-8");$id=$_GET['maxid'];$data = mysqli_query($link, "select * from talk where id>$id");while ($array = mysqli_fetch_assoc($data)) { $info[] = $array;};  echo json_encode($info);

3. chat.sql

DROP TABLE IF EXISTS `talk`;CREATE TABLE `talk` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', `send` varchar(10) CHARACTER SET utf8 DEFAULT NULL COMMENT '寄件者暱稱', `ip` varchar(12) CHARACTER SET utf8 DEFAULT NULL, `content` varchar(500) CHARACTER SET utf8 DEFAULT NULL, `time` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;

4. add.php

<?php /** * @功能 將表單插入資料庫 */$content=$_POST['content'];print_r($_POST);$time= date("Y-m-d H:i:s", time(0));$link = mysqli_connect('localhost', 'root', '123', 'test');mysqli_query($link, 'set names utf8');$ip = $_SERVER["REMOTE_ADDR"];$sql="INSERT INTO talk VALUES(NULL,'匿名者','$ip','$content','$time')";$data = mysqli_query($link,$sql);echo "$content";if($data) echo "1"; else  echo "0";

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.