user1332901
user1332901

Reputation:

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

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!

Upvotes: -1

Views: 10234

Answers (2)

Raj Nandan Sharma
Raj Nandan Sharma

Reputation: 3852

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

Upvotes: 0

Oluwakayode Dagbo
Oluwakayode Dagbo

Reputation: 191

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

Upvotes: 0

Related Questions