ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Creating a chat script with PHP and Ajax, Part 2

(Page 2 out of 5)

The basic necessities

The first thing our web app needs is a 'global.php' file, which will setup a database connection, include all the other files and libraries and do a few other basic tasks. It looks like this:


error_reporting (E_ALL);

if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
        $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

// Get site path
$site_path = realpath(dirname(__FILE__) . '/../') . '/';

// Include libraries
include ($site_path . 'libs/JSON.php');
include ($site_path . 'libs/adodb/adodb.inc.php');

include ($site_path . 'includes/functions.php');
include ($site_path . 'includes/user-functions.php');
include ($site_path . 'includes/chat-functions.php');

// Connect to MySQL DB
$db = NewADOConnection('mysql');
$db->Connect('localhost', 'DB_USER_HERE', 'DB_PASSWORD', 'DB_NAME');
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

?>

Make sure to put your own database user, password and name in the file above, and save it as 'global.php' in the includes directory. The web app will also need a common 'functions.php', which holds frequently used functions like generate_password() and such. Click here to download the 'functions.php' and save it in the includes directory.

Now that we've got the basics pretty much laid out, let's start with the actual web app. The first thing we'll do is create the chat client itself.

Creating the chat client

To start off, our chat client only needs to do three things:
1. Let the visitor enter a username and login
2. Show all the incoming messages, and allow new messages to be sent
3. Keep a list of who's in the chat room

The HTML behind these three things is fairly standard:


       
                </span>PHP-Based Live Ajax Chat<span class="sc2"><span class="kw2">

                rel="stylesheet" href="style.css" type="text/css" media="screen" />

                type="text/javascript">
                        var base_url = document.location.protocol + '//' + document.location.host + document.location.pathname;
                        var username = '';
                        var password = '';
                        var lastactive = '';
                        var dup_filter = new Array();
               

               
                type="text/javascript" src="libs/prototype.js">
                type="text/javascript" src="libs/json.js">
                type="text/javascript" src="libs/logger.js">
                type="text/javascript" src="js/common.js">

                 
                type="text/javascript" src="js/user.js">
                type="text/javascript" src="js/ping.js">
                type="text/javascript" src="js/chat.js">

       

       

        id="login">
               

Enter a username to enter the chatroom...


                for="username">Username:
                type="text" name="username" id="username" />

                href="#" onclick="login();">Enter the chat room »
       

        id="chat" style="display:none;">
                id="messages">
                        Welcome to the chatroom...
                       
               


               
                id="userlist">
                       

Users:


                        id="users">
                               
                       
               

               

                type="text" id="talk" name="talk"> type="submit" value="Say It!" onclick="send_message();" id="sayit" />

       

       

The above page uses fairly basic HTML, and consists mainly of two div elements ("login" and "chat"), a few input elements and a list element for the user list. The page also includes several JavaScript files, including the Prototype, JSON and Logger libraries. To make the chat room look a bit prettier, an external stylesheet is used, which can be downloaded by clicking here.

If you try to use the page above, you'll find that nothing works yet, because we haven't written any of our custom JS files yet. But that's what we'll do now, starting with the login.

« Previous: Introduction & Structure
Next: Creating the login »



This article was posted on Monday, April 3rd, 2006 at 9:53 pm. You can follow any responses to this article through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

8 Responses to “Creating a chat script with PHP and Ajax, Part 2”

  1. AjaxBlog.it » Creare una chat con PHP ed Ajax Says:

    […] Tra le moltissime letture, mi ero dimenticato di far presente del primo di una serie di articoli dedicati alla creazione di una chat utilizzando il linguaggio di programmazione PHP ed ovviamente Ajax. Be’, lo faccio ora insieme all’annuncio dell’uscita della seconda parte. […]

  2. Matthijs Says:

    Very interesting article. Will certainly download the code and study it some more. Thanks.

    One question though: how is the escaping of the data to the db handled? I didn’t study all code yet, but in your article in the login script there’s only the code
    // Make it safe
    $username = htmlentities($username);
    before the data is sent to the db.
    Does ADODB handle the escaping itself?

  3. Bernhard Froehlich Says:

    It’s a good example for what can be done with Ajax but it’s no real world example. Webchats are supposed to serve at least 1000 Clients but this thing will kill the Server if there are more than ~100 and i testet the live demo - it’s having a lag of about 2sec and thats not good in real world.

    But nevertheless a nice example.

  4. berhard kuisper Says:

    Its realy good chat but this is php. I code java chats and i have mor than 2000 users in this system , but with php server was slow or down.

    Sorry java is greater for chatsystems or php for a alternativ session!

    Best regards

  5. Aaron Hancock Says:

    Bernhard, I think the delay is due to the refresh/reload rate and not server lag. With a few tweaks, this would make a great small scale chat client.

  6. www.stuffvideo.com » Ajax article Says:

    […] http://phpit.net/article/ajax-php-chat-part-two/1/ […]

  7. Michael Says:

    There are some Problems with UTF-8 with German Umlaute (ä ü ö). How can i handle this?

  8. Prasanna Says:

    Hi

    This is a gr8!!!!!!!!!! article…

    Will download the code to dwell deep into it.

    Thanks
    Prasanna

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Structure
  2. Basic Necessities & Chat Client
  3. Creating the login
  4. Ping? Pong!
  5. Sending new messages
Bookmark Article
Download Article
PDF
Download this article as a PDF file