Custom ASP or .NET Site

Do not know programming? Want us to perform the installation?

Simply purchase our Premium or Platinum Edition and our team will perform the complete integration and installation. That’s right! All the steps below will be performed by our team and all you need to do is sit back and relax.

Please create a support ticket for the PHP + MSSQL version of CometChat. The default adapter is PHP + mySQL.

Server Setup

* Install PHP on your IIS server using the tool at http://php.iis.net
* Install the SQLSRV driver for PHP to allow PHP to connect to MSSQL from http://www.microsoft.com/en-in/download/details.aspx?id=20098

Code Modification

The only modification that needs to be performed in your existing ASP or .NET code is to add a cookie named userid (which will store the id of the logged in user) when the user logs in and reset it to 0 when the user logs out.

Configuration

Switch on development mode

Edit config.php and search for the tag:

/* ADVANCED */

Set DEV_MODE to 1, ERROR_LOGGING to 1 and CACHING to 0. After integration, you can set it to default.

define('DEV_MODE',setConfigValue('DEV_MODE','1'));
define('ERROR_LOGGING',setConfigValue('ERROR_LOGGING','1'));
define('MEMCACHE',setConfigValue('MEMCACHE','0')); 

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');
$table_prefix = '';
$db_usertable = 'users';	
$db_usertable_userid = 'userid';
$db_usertable_name = 'username';
$db_avatartable = ' ';
$db_avatarfield = ' '.$table_prefix.$db_usertable.'.'.$db_usertable_userid.' ';

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

The next 6 lines specify details about your database schema.

* If all your tables use a prefix, for example tableprefix_, then specify the $table_prefix as tableprefix_ or you can leave it empty and mention complete table name while defining $db_usertable
* $db_usertable specifies the name of the table in which your user information is stored.
* $db_usertable_name specifies the name of the field from users table in which the user’s name/display name is stored.
* $db_usertable_userid specifies the name of the field from users table 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.
* $db_avatartable specifies join of users table with avatar table. If avatar is stored in the users table then leave this field blank.
* $db_avatarfield is the user ID by default. You can change this if your site stores avatar in users or avatars table.

The code will look like:

$table_prefix = '';
$db_usertable = 'users';	
$db_usertable_userid = 'userid';
$db_usertable_name = 'username';
$db_avatartable = ' ';
$db_avatarfield = ' '.$table_prefix.$db_usertable.'.'.$db_usertable_userid.' ';

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. Since you have modified the code to create a cookie named userid, we will use this cookie to get userid in CometChat.

So the function will look like:

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

Update Who’s Online list fetching mechanism

We need to 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
status 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    status
--------------------------------
13       1         2       1
14       2         1       1
--------------------------------

To show only your friends in the Who’s Online list, add the following in your getFriendsList():

function getFriendsList($userid,$time) {
 $sql = ("select users.id userid, users.username username, users.link link, users.avatar avatar, cometchat_status.lastactivity lastactivity,  cometchat_status.lastseen, cometchat_status.lastseensetting,  cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from friends join users on friends.toid = users.id left join   cometchat_status on users.id = cometchat_status.userid where friends.fromid = '".mssql_real_escape_string($userid)."' order by username asc");
 return $sql;
}

If you wish to show all users, add the following code in your getFriendsList():

function getFriendsList($userid,$time) {
 $sql = ("select DISTINCT users.id userid, users.username username,  users.username link,  users.id  avatar, cometchat_status.lastactivity lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from users left join cometchat_status on users.id = cometchat_status.userid order by username asc");
 return $sql;
}

However, we do not recommend this, since it will add load to your site.

If you would like to show only online users, then you can use:

function getFriendsList($userid, $time){
 $sql = ("select userid, username, link, avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from left join cometchat_status on = cometchat_status.userid where (('" . mssql_real_escape_string($time) . "' - cometchat_status.lastactivity < '".((ONLINE_TIMEOUT)*2)."') OR cometchat_status.isdevice = 1) and (cometchat_status.status IS NULL OR cometchat_status.status <> 'invisible' OR cometchat_status.status <> 'offline') order by username asc");
 return $sql;
}

Update user information fetching mechanism

Your getUserDetails() will look like:

function getUserDetails($userid){
 $sql = ("select userid, username, link, avatar, cometchat_status.lastactivity, cometchat_status.lastseen,cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from left join cometchat_status on userid = cometchat_status.userid where userid = '" . mssql_real_escape_string($userid) . "'");
 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 fetchLink() 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). fetchLink() and getAvatar() functions are used to post-process the data obtained from the getFriendsList() function.

For example:

function fetchLink($link) {
	 return BASE_URL.'../users.php?id='.$link;
}
	
function getAvatar($image) {
    if (is_file(dirname(dirname(__FILE__)).'/images/'.$image.'.gif')) {
	 return BASE_URL.'../images/'.$image.'.gif';
    } else {
	 return BASE_URL.'images/noavatar.png';
    }
}

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.

Adding compatibility with Mobile apps and Desktop Messenger (Optional)

Your chatLogin() will look like:

function chatLogin($userName,$userPass) {
   $userid = 0;	
   $sql = ("SELECT * FROM users WHERE username='".$userName."'");
   $result = sqlsrv_query($GLOBALS['dbh'],$sql);
   $row = sqlsrv_fetch_array($result);	
   $check = md5(md5($userPass));
   if ($check==$row['password']) { 
      $userid = $row['user_id'];
      if (isset($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp') {
        $sql = ("insert into cometchat_status (userid,isdevice) values ('".mssql_real_escape_string($userid)."','1') on duplicate     key update isdevice = '1'");
mysqli_query($GLOBALS['dbh'], $sql);
      }
   }
 if (function_exists('mcrypt_encrypt') && defined('ENCRYPT_USERID') && ENCRYPT_USERID == '1') {
    $key = KEY_A.KEY_B.KEY_C;
       $userid = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $userid, MCRYPT_MODE_CBC, md5(md5($key))));
    }
  return $userid;
}

The chatLogin() returns user id of logged in user if the username and password are authenticated successfully.

In your getUserID(), add the following code after $userid = 0;

if (!empty($_SESSION['basedata']) && $_SESSION['basedata'] != 'null') {
  $_REQUEST['basedata'] = $_SESSION['basedata'];
}
if (!empty($_REQUEST['basedata'])) {
 if (function_exists('mcrypt_encrypt')) {
   $key = KEY_A.KEY_B.KEY_C;
   $uid = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($_REQUEST['basedata']), MCRYPT_MODE_CBC, md5(md5($key))), "");
     if (intval($uid) > 0) {
      $userid = $uid;
     }
  } else {
   $userid = $_REQUEST['basedata'];
  }
} 

So, your getUserID() will look like:

function getUserID() {
 $userid = 0;
 if (!empty($_SESSION['basedata']) && $_SESSION['basedata'] != 'null') {
   $_REQUEST['basedata'] = $_SESSION['basedata'];
 }
 if (!empty($_REQUEST['basedata'])) {
   if (function_exists('mcrypt_encrypt')) {
    $key = KEY_A.KEY_B.KEY_C;
    $uid = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($_REQUEST['basedata']), MCRYPT_MODE_CBC, md5(md5($key))), "");
      if (intval($uid) > 0) {
       $userid = $uid;
      }
    } else {
    $userid = $_REQUEST['basedata'];
    }
   }
 if (!empty($_COOKIE['userid'])){
   $userid = $_COOKIE['userid'];
 }
 return $userid;
}

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.

Adding Cache support

Before you begin with this step, please enable one of the caching options from CometChat Admin Panel.

Then you need to modify the getFriendsList() and getFriendsIds() in integration.php file.

a. Show all online users with caching enabled

To use caching for all online users, no change is required; simply use the getFriendsList function from above (Update Who’s Online list fetching mechanism).

b. Show online friends with caching enabled

When using caching and wanting to show only online friends, you still need to modify the getFriendsList function to show all online users as follows.

function getFriendsList($userid, $time){
  $sql = ("select userid, username, link, avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message,      cometchat_status.isdevice from left join cometchat_status on = cometchat_status.userid where (('" . mysqli_real_escape_string($GLOBALS['dbh'],     $time) . "' - cometchat_status.lastactivity < '".((ONLINE_TIMEOUT)*2)."') OR cometchat_status.isdevice = 1) and    (cometchat_status.status IS NULL OR cometchat_status.status <> 'invisible' OR cometchat_status.status <> 'offline') order by username    asc");
  return $sql;
}

Then you need to modify getFriendsIds to return friends for the logged in user. The getFriendsIds() function should return SQL query which retrieves userids of friends.

function getFriendsIds($userid) {
  $sql = ("select toid friendid from friends where fromid ='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status = 1 union select   fromid friendid from friends where toid='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status=1");
  return $sql;
}

The getFriendsList function helps to store the details of all the online users in a cache and getFriendsIds helps to store only ids of all the friends in a cache. CometChat then uses both the caches to get the details of all the friends.

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.

Not finding what you need?

The CometChat team is here to help!