User Management

Product:
Edit integration.php and update the database details.

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

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

1) $db_usertable specifies the name of the table in which your user information is stored.
2) $db_usertable_name specifies the name of the field from users table in which the user’s name/display name is stored.
3) $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.
4) $db_avatartable specifies join of users table with avatar table. If avatar is stored in the users table then leave this field blank.
5) $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 Who’s Online list fetching mechanism

We need to modify the getFriendsList() function which is in integration.php file.

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 = '".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' order by username asc");
 return $sql;
}
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;
}
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 = '".sql_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;
}
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;
}
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 above code, since it will fetch all users and add load on your web site.

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

function getFriendsList($userid, $time){
 $sql = ("select users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.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 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;
}

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;
}

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 (('" . sql_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 users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from users left join cometchat_status on userid = cometchat_status.userid where userid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "'");
 return $sql;
}
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;
}
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 = '" . sql_real_escape_string($userid) . "'");
 return $sql;
}

The getUserStatus() function returns the current status message as well as the 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 which is in integration.php file.

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.

User management can be done in two ways. The quickest is to use the On-the-fly Method. If you have a friends system and would like to show only friends in the Contacts tab, then you will have to use our Restful API.

Option 1: On-the-fly User Creation Method

You can create users on-the-fly and log them into CometChat:

<script>
var chat_name = 'LOGGEDIN_USERS_NAME';
var chat_id = 'LOGGEDIN_USERS_UNIQUE_ID';
var chat_avatar = 'LOGGEDIN_PROFILE_IMAGE';
var chat_link = 'LOGGEDIN_USERS_PROFILE_LINK';
</script>

Where:
chat_name: Should store the name of the logged in user.
chat_id: Should be unique, numeric, not null value which stores the logged in user’s ID.
chat_avatar (optional): Should store the complete path of the profile picture of the logged in user.
chat_link (optional): Should store the profile link of the logged in user.

Option 2: Restful API User Management Method

You will have to use our Restful API to manage users.

  • Was this helpful?
  • Yes   No