Create a web-based chat box using AJAX, PHP, and SQL Long Polling?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 12:44:38

问题


I'm creating an online chat box for me and my friends at college to use online. In the current configuration, the chat messages are stored in a plain text file on the host machine (mine) and are fetched via AJAX every second, however, sometimes it is slow and glitchy and doesn't always work.

To send a message, it passes the message to a JavaScript function which passes the value to a PHP script, writing it to the file along with the user's unique color (stored in a local cookie). Here's the functions.js file (pastebin): http://pastebin.com/CpGxj5cP

Here's the php file to send the message:

<?php
session_start();
require_once('mysql_connect.php');
date_default_timezone_set("EST");

//Format the message
$date = date('n/j g:i A');
$username = $_SESSION['username'];
$color = $_COOKIE[$username];

$message  = "<font color='" . $color . "'>" . $username . "</font> (" .  $date . "): ";
$message .= $_GET['m'] . "\n";

$file = '../messages.txt';

$handle = fopen($file, 'a');
fputs($handle, $message);

fclose($handle);

//Reset timeout
//$_SESSION['timeout'] = 300;
?>

As I said above, the issue is that it's very very slow. If there's a way to do it better than a textfile/AJAX, please let me know!


回答1:


Yes there is a better way if you are using a browser that supports HTML 5

Web Sockets

http://www.tutorialspoint.com/html5/html5_websocket.htm




回答2:


PHP/Ajax chat source is available on http://frug.github.io/AJAX-Chat/

  • Download Latest Standalone ZIP from this site.

  • Unzip downloaded file.

  • Read the readme.txt file from unzipped folder to configure.

Enjoy it.




回答3:


Check this out for a full code of a chat box using PHP. Download the source code or see the live demo in this site.

Moderator note: This link is no longer alive, and archive.org does not seem to have a copy, either. http://purpledesign.in/blog/?p=19

`    function getLoginBox() {
    ob_start();
    require_once('login_form.html');
    $sLoginForm = ob_get_clean();

    $sLogoutForm = '<a href="'.$_SERVER['PHP_SELF'].'?logout=1">logout</a>';

    if ((int)$_REQUEST['logout'] == 1) {
        if (isset($_COOKIE['member_name']) && isset($_COOKIE['member_pass']))
            $this->simple_logout();
    }

    if ($_REQUEST['username'] && $_REQUEST['password']) {
        if ($this->check_login($_REQUEST['username'], MD5($_REQUEST['password']))) {
            $this->simple_login($_REQUEST['username'], $_REQUEST['password']);
            return 'Hello ' . $_REQUEST['username'] . '! ' . $sLogoutForm;
        } else {
            return 'Username or Password is incorrect' . $sLoginForm;
        }
    } else {
        if ($_COOKIE['member_name'] && $_COOKIE['member_pass']) {
            if ($this->check_login($_COOKIE['member_name'], $_COOKIE['member_pass'])) {
                return 'Hello ' . $_COOKIE['member_name'] . '! ' . $sLogoutForm;
            }
        }
        return $sLoginForm;
    }
}`

Of course you will need to create a log in box. Kindly check the link I have shared. It has all the details



来源:https://stackoverflow.com/questions/18860054/create-a-web-based-chat-box-using-ajax-php-and-sql-long-polling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!