This guide will help you through the installation process and get CometChat running on your site.
Installation is very straight forward, only taking about 15 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).
You do not need programming knowledge to install CometChat, but a working knowledge of FTP functions is a plus.
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 don't know already, find out the folder in which Standalone site is installed.
If you access your Standalone site site via '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 inside your Standalone site folder. e.g.
www.domain.com/cometchat
Configuration
1. 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');
2. 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.
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 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
3. 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 another language like ASP, ColdFusion, Python etc. then set a cookie immediately after login with the user's ID. You can 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'])."'"Zwinker;
$query = mysql_query($sql);
$session = mysql_fetch_array($query);
$userid = $session['userid'];
}
return $userid;
}
4. 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
----------------------------
function getFriendsList($userid,$time) {
$sql = ("select DISTINCT ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." userid,
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_NAME. " username,
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_LASTA CTIVITY." lastactivity,
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." avatar, ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." 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_USERI D."
left join cometchat_status on ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." = cometchat_status.userid
where ".TABLE_PREFIX."friends.fromid = '".mysql_real_escape_string($userid)."'
order by username asc"Zwinker;
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_USERI D." userid,
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_NAME. " username,
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_LASTA CTIVITY." lastactivity,
".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." avatar, ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." link,
cometchat_status.message, cometchat_status.status
from ".TABLE_PREFIX.DB_USERTABLE." left join cometchat_status on ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." = cometchat_status.userid
where ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERI D." <> '".mysql_real_escape_string($userid)."' and ('".$time."'-".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_LASTA CTIVITY."<120)
order by username asc"Zwinker;
return $sql;
}
5. 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
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.
Thank you for purchasing CometChat!