使用 PHP 进行聊天的资源

发布于 2024-10-10 11:36:53 字数 202 浏览 4 评论 0原文

我将开发一个简单的聊天网站来帮助我的朋友网站,该网站需要聊天支持功能,并且因为我喜欢测试我的 PHP 知识,但首先我需要一些可以开始的资源。我想从一些非常非常简单的事情开始,然后开始改进它(因为我通常喜欢做东西)。

那么你能分享一些我需要做这个的资源吗?喜欢示例、片段和文档吗?

PS:我不太擅长 Javascript,也不擅长 AJAX

I'm going to develop a simple chat site to help my friend site, that needs a chat support feature and because I like to test my PHP knowledge, but first I'm needing some resources where I can start from. I want to start with something very very very simple, then I start to improve it(as I usually like to make things).

So can you share some resources that I will need to make this? Like examples, snippet and docs?

PS: I'm not so good with Javascript neither AJAX

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2024-10-17 11:36:53

由于您要求一个非常非常简单的解决方案,这里有一个充满缺陷的工作示例。

问候,

//t

<?
//chat.php
//collect input from user
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  //submitted by form - add to conversation...
  //get conversation from file in server to array..
  if (!file_exists('conversation.txt'))
    file_put_contents('conversation.txt',serialize(array()));
  $a = unserialize(file_get_contents('conversation.txt'));
  //add the new entry to the conversation
  $a[] = array(
    'user' => $_POST['user'],
    'line' => $_POST['line']
  );
  //if conv. > 20 msg, crop
  if (sizeof($a)>20) $a = array_split($a, sizeof($a)-20);
  //now save..
  file_put_contents('conversation.txt',serialize($a)); 
  //and we're done with logic..
}
//from here, only presentation..
?>
<html>
<body>
...
<?php
//show conv...
$a = unserialize(file_get_contents('conversation.txt'));    
for($i=0;$i<sizeof($a);$i++) {
  echo $a[$i]['user'] . ':' . $a[$i]['line'] . '<br />';
}
?>
//build a form to submit line...
<form name="frm1" method="post" action="chat.php">
  <input type="text" name="user" />
  <input type="text" name="line" size="60" />
  <input type="submit"/>
</form>
<!-- add js that reloads as long as no entry in line... !-->
<script type="text/javascript">
function getChat() {
  if (document.frm1.line.value.length == 0) {
    //alert('reloading');
    location.reload(true);
  }
}
//add a simple js-timer to fire every 8 sec.
setInterval('getChat()',8000);
</script>
</body>
</html>

Since you requested a very very very simple solution, here's a full-of-flaws-working-example.

regards,

//t

<?
//chat.php
//collect input from user
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  //submitted by form - add to conversation...
  //get conversation from file in server to array..
  if (!file_exists('conversation.txt'))
    file_put_contents('conversation.txt',serialize(array()));
  $a = unserialize(file_get_contents('conversation.txt'));
  //add the new entry to the conversation
  $a[] = array(
    'user' => $_POST['user'],
    'line' => $_POST['line']
  );
  //if conv. > 20 msg, crop
  if (sizeof($a)>20) $a = array_split($a, sizeof($a)-20);
  //now save..
  file_put_contents('conversation.txt',serialize($a)); 
  //and we're done with logic..
}
//from here, only presentation..
?>
<html>
<body>
...
<?php
//show conv...
$a = unserialize(file_get_contents('conversation.txt'));    
for($i=0;$i<sizeof($a);$i++) {
  echo $a[$i]['user'] . ':' . $a[$i]['line'] . '<br />';
}
?>
//build a form to submit line...
<form name="frm1" method="post" action="chat.php">
  <input type="text" name="user" />
  <input type="text" name="line" size="60" />
  <input type="submit"/>
</form>
<!-- add js that reloads as long as no entry in line... !-->
<script type="text/javascript">
function getChat() {
  if (document.frm1.line.value.length == 0) {
    //alert('reloading');
    location.reload(true);
  }
}
//add a simple js-timer to fire every 8 sec.
setInterval('getChat()',8000);
</script>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文