My Account | Buy CometChat

documentation

Custom PHP Site

Introduction

This guide will help you through the installation process and get CometChat running on your site.

Installation is very straight forward, only taking about 30 minutes from uploading the files to viewing the CometChat bar on your site.

Before you begin, you will need an FTP client, if you do not have one, some popular solutions include FileZilla (free) or CuteFTP (trial).

This guide assumes that you have successfully downloaded the latest release of CometChat and have the zip file “unzipped” and ready to go. If not, you can download the package from your client area.

The instructions will term the zip file you downloaded as cometchat.zip.

Uploading

At this point, you should have the zip archive cometchat.zip and find a single folder- “cometchat”.

If you access your site via ‘http://www.domain.com’, then look for the webroot directory.

The “webroot” directory is usually ‘public_html’ or ‘www’, but this varies from server to server so if you’re unsure, contact your hosting provider.

Using your FTP client, copy the cometchat folder to your webroot directory. e.g. http://www.domain.com/cometchat

Configuration

Switch on development mode

Edit config.php and search for the tag:

/* ADVANCED */

Set DEV_MODE to 1 and ERROR_LOGGING to 1. After integration, you can set these to 0.

define('DEV_MODE','1');
define('ERROR_LOGGING','1');

Add your database information

Edit integration.php and update the DATABASE details.

/* DATABASE */

define('DB_SERVER','localhost');
define('DB_PORT','3306');
define('DB_USERNAME','root');
define('DB_PASSWORD','password');
define('DB_NAME','databasename');
define('TABLE_PREFIX','');
define('DB_USERTABLE','users');
define('DB_USERTABLE_NAME','username');
define('DB_USERTABLE_USERID','userid');
define('DB_USERTABLE_LASTACTIVITY','lastactivity');

The first 5 lines are used to access the database. You must access your existing database and not create a separate database for CometChat.

The next 5 lines specify details about your database schema.

  • If all your tables use a prefix, for example cc_, then specify the TABLE_PREFIX as cc_
  • DB_USERTABLE specifies the name of the table in which your user information is stored.
  • DB_USERTABLE_NAME specifies the name of the field in which the user’s name/display name is stored.
  • DB_USERTABLE_USERID specifies the name of the field in which the user’s id is stored (usually id or userid or user_id or member_id). This field must be of integer type.

The code will look like:

define('TABLE_PREFIX','tableprefix_');
define('DB_USERTABLE','userstable');
define('DB_USERTABLE_NAME','username');
define('DB_USERTABLE_USERID','userid');

In order to keep track of which users are online, CometChat makes use of an integer field which stores a UNIX timestamp (which is an INTEGER) of the user’s last activity. If you do not have such a field in your user’s table as yet, you can execute the following SQL command:

ALTER TABLE users ADD lastactivity INTEGER DEFAULT 0; // Update "users" to your users' table name

Update single-sign-on functionality

Scroll down to find the getUserID() function.

The getUserID() function is used to return the logged in user’s ID. Depending on how you have programmed your site, you will have to find out the user’s ID.

If you have written a simple PHP authentication, then during the authentication, you can specify a session variable like:

/* In your own login.php */
/* After you authenticate the user */
$_SESSION['userid'] = $users['id']; // Modify to suit requirements

Then your getUserID() function will look like:

function getUserID() {
    $userid = 0; // Return 0 if user is not logged in

    if (!empty($_SESSION['userid'])) {
        $userid = $_SESSION['userid'];
    }

    return $userid;
}

If you are using a cookie then read that using the following function:

function getUserID() {
    $userid = 0; // Return 0 if user is not logged in

    if (!empty($_COOKIE['userid'])) {
        $userid = $_COOKIE['userid'];
    }

    return $userid;
}

If you are using a more complex method of authentication like storing the session_hash in the database, then your getUserID() function will look something like:

function getUserID() {
    $userid = 0; // Return 0 if user is not logged in

        if (!empty($_COOKIE['sessionhash'])) {
        $sql = ("select userid from ".TABLE_PREFIX."session 
            where sessionhash = '".mysql_real_escape_string($_COOKIE['sessionhash'])."'");
        $query = mysql_query($sql);
        $session = mysql_fetch_array($query);
        $userid = $session['userid'];
        }

    return $userid;
}

Update Who’s Online list fetching mechanism

We modify the getFriendsList() function.

The getFriendsList() function returns all the users’ details part of the Who’s Online list. This list need not necessarily be friends of the logged in user. It may simply be all online users instead. You can customize this to return any set of users.

The default configuration assumes that you have a table called friends with the following fields:

toid integer
fromid integer

All entries are assumed to be two-way i.e. if user A (id:1) and user B (id:2) are friends, then there will be two entries in the table:

----------------------------
 id    toid    fromid
----------------------------
 13       1         2
 14       2         1
----------------------------

Your getFriendsList() will look like:

function getFriendsList($userid,$time) {
    $sql = ("select DISTINCT ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." userid, 
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_NAME." username, 
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_LASTACTIVITY." lastactivity, 
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." avatar, ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." link, 
cometchat_status.message, cometchat_status.status 
from ".TABLE_PREFIX."friends join ".TABLE_PREFIX.DB_USERTABLE." 
on  ".TABLE_PREFIX."friends.toid = ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." 
left join cometchat_status on ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." = cometchat_status.userid 
where ".TABLE_PREFIX."friends.fromid = '".mysql_real_escape_string($userid)."' 
order by username asc");
    return $sql;
}

If you would like to show all online users instead, then you can use an SQL query like:

function getFriendsList($userid,$time) {
    $sql = ("select DISTINCT ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." userid, 
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_NAME." username, 
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_LASTACTIVITY." lastactivity, 
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." avatar, ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." link, 
cometchat_status.message, cometchat_status.status 
from ".TABLE_PREFIX.DB_USERTABLE." left join cometchat_status on ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." = cometchat_status.userid 
where ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." <> '".mysql_real_escape_string($userid)."' and ('".$time."'-".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_LASTACTIVITY."<120)
order by username asc");
    return $sql;
}

Update status message, avatar and links functionality

The getUserStatus() function returns the current status message as well as state of the user (available, busy, invisible, offline). If your site already has a status updates feature, then you will have to modify the first field- cometchat_status.message which is returned to pull the status message from your table.

Finally, we modify getLink() and getAvatar() function

When the getFriendsList() function is executed, avatar and link contain the user id by default (you can change this if you store the avatar/link location in your table). These functions are used to post-process the data obtained from the getFriendsList() function.

For example:

function getLink($link) {
    return 'users.php?id='.$link;
}

function getAvatar($image) {
    if (is_file(dirname(dirname(__FILE__)).'/images/'.$image.'.gif')) {
        return '/images/'.$image.'.gif';
    } else {
        return '/images/noavatar.gif';
    }
}

The hooks_statusupdate() function is called when a user updates his/her status via CometChat. If your site already has a status updates feature, you can update that using this hook.

Installation

  • Step 1

You should now run the installer file through your web browser by entering the URL to it into your browser address bar (if you have followed our example, type in http://www.domain.com/cometchat/install.php, naturally substituting ‘domain.com’ for your web address).

If the installation was completed successfully, then two lines of HTML code will be displayed on your screen. Please copy these two lines.

  • Step 2

Now edit your template header.

Immediately after the opening head tag add the copied code

Now delete install.php file from the cometchat folder.

That’s all! Now log-in to your site and you will be able to see the CometChat bar. For customizing the text, icons, plugins, theme and modules, please refer to other documents.