Powerful Chat System – Lesson 9

Powerful Chat System – Lesson 9

Powerful Chat System - Lesson 9
Powerful Chat System

Powerful Chat System – Lesson 9

Today I prepared new stage of our php/mysql based chat system. In our ninth lesson I added one important feature: public rooms. Now your members can talk in different public rooms. Right to create and remove rooms belong to administrators.

Today, as usual, we will publish only updated sources of our project. As you know, whole project is well structured: system classes is in ‘classes’ folder, all javascript files in ‘js’ folder, stylesheets in ‘css’ folder, all custom avatars in ‘data’ folder, images in ‘images’ folder, template files in ‘templates’ folder.

Live Demo

Now – download the source files and lets start coding !


Step 1. SQL

I added one new table into our database ‘cs_rooms’. This table keeps records about rooms. Please execute next SQL:

CREATE TABLE `cs_rooms` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `title` VARCHAR(255) NOT NULL,
  `owner` int(11) unsigned NOT NULL,
  `when` int(11) NOT NULL default '0',
  PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `cs_rooms` (`id`, `title`, `owner`, `when`) VALUES
(NULL, 'Room1', 3, 1338293810),
(NULL, 'Room2', 3, 1338293811),
(NULL, 'Room3', 3, 1338293812);

We have just added three rooms.

Step 2. HTML

I updated template of our main chat box. I added current room id in two places:

templates/chat.html

<form class="chat_submit_form">
    <div><input type="text" name="message" /><input type="submit" value="Submit" name="Submit" /></div>
    <div>
        <h3 class="error">Some error occurs during sending message</h3>
        <h3 class="success">Message successfully sent</h3>
        <h3 class="protect">Please wait 5 secs before adding next message</h3>
    </div>
    <input type="hidden" name="room" value="{room}" />
</form>
<script>
    var iRoom = {room};
</script>
<script src="js/chat.js"></script>

Our main page is affected too, There are several changes regarding rooms feature:

templates/main_page.html

<!DOCTYPE html>
<html lang="en" >
<head>
    <title>Powerful Chat System - Lesson 9</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <link href="css/rooms.css" rel="stylesheet" type="text/css" />
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
    <header>
        <h2>Powerful Chat System - Lesson 9</h2>
        <a href="http://www.script-tutorials.com/powerful-chat-system-lesson-9/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
    </header>
    <div class="clear"></div>
    <div class="container" id="con1">
        {form}
        {avatar}
    </div>
    <div class="container" id="con2">
        {rooms}
        {new_room}

        <h2>{chat_name} Chat Block</h2>
        <div class="chat_messages">
            {chat}
        </div>
        {input}
    </div>

    <div class="sidebar">
        <div>
            <h2>Online Members Block</h2>
            {online_members}
        </div>
        <div>
            <h2>Last Members</h2>
            {profiles}
        </div>
    </div>

    <div class="priv_dock_wrap"></div>
    {priv_js}

</body>
</html>

And finally I added one new template file to manage with rooms (where we can create or remove current active room)

templates/new_room.html

<form class="new_room_form" method="post">
    <div><input type="text" name="title" /><input type="submit" value="Add room" name="submit" /></div>
    <input type="hidden" name="action" value="add_room" />
</form>
<form class="delete_room_form" method="post">
    <input type="submit" value="Delete current room" name="submit" />
    <input type="hidden" name="room_id" value="{room}" />
    <input type="hidden" name="action" value="delete_room" />
</form>
<div class="clear"></div>
<br /><hr />

Step 3. CSS

Of course, we should stylize the tabs for our rooms (each room – css tab):

css/rooms.css

.roomsHolder {
    font: bold 12px/35px verdana;
    overflow: hidden;
    position: relative;
}
.roomsHolder .shadow {
    background-color: #888888;
    height: 8px;
    left: 5%;
    position: absolute;
    top: -9px;
    width: 90%;

    /* css3 box shadow */
    -webkit-box-shadow: 0 0 10px #000000;
    -moz-box-shadow: 0 0 10px #000000;
    -o-box-shadow: 0 0 10px #000000;
    box-shadow: 0 0 10px #000000;
}
ul.rooms {
    display: block;
    float: right;
    height: 70px;
    list-style: none outside none;
    margin: 0;
    padding: 0 60px;
    position: relative;
}
ul.rooms li {
    float: left;
    margin: 0 5px 0 0;
}
ul.rooms li a {
    color: #DDDDDD;
    display: block;
    padding: 0 10px;
    text-decoration: none;

    /* css3 box shadow */
    -webkit-box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.9);
    -moz-box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.9);
    -o-box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.9);
    box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.9);

        /* css radius */
    -webkit-border-bottom-right-radius: 10px;
    -webkit-border-bottom-left-radius: 10px;
    -moz-border-radius: 0 0 10px 10px;
    -o-border-radius: 0 0 10px 10px;
    border-radius: 0 0 10px 10px;

        /* css3 transition */
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}
ul.rooms li a.red {
    background-color:#a00;
}
ul.rooms li a.orange {
    background-color:#da0;
}
ul.rooms li a.green {
    background-color:#060;
}
ul.rooms li a.blue {
    background-color:#00a;
}
ul.rooms li a.indigo {
    background-color:#2b0062;
}
ul.rooms li a.violet {
    background-color:#682bc2;
}
ul.rooms li a:hover, ul.rooms li a.active {
    background:#aaa;
    color:#fff;
    padding:10px 10px 0 10px;
}
ul.rooms li a.red:hover, ul.rooms li a.red.active {
    background-color:#c00;
}
ul.rooms li a.orange:hover, ul.rooms li a.orange.active {
    background-color:#fc0;
}
ul.rooms li a.green:hover, ul.rooms li a.green.active {
    background-color:#080;
}
ul.rooms li a.blue:hover, ul.rooms li a.blue.active {
    background-color:#00c;
}
ul.rooms li a.indigo:hover, ul.rooms li a.indigo.active {
    background-color:#5b1092;
}
ul.rooms li a.violet:hover, ul.rooms li a.violet.active {
    background-color:#8a2be2;
}
.new_room_form {
    float: left;
}
.delete_room_form {
    float: right;
}

Step 4. PHP

Index page file was updated. I added all necessary functional to work with rooms: admins can create and remove rooms. Usual members (and admins too) can write messages into different rooms.

index.php

<?php

// set error reporting level
if (version_compare(phpversion(), '5.3.0', '>=') == 1)
  error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
else
  error_reporting(E_ALL & ~E_NOTICE);

require_once('classes/Services_JSON.php');
require_once('classes/CMySQL.php'); // including service class to work with database
require_once('classes/CLogin.php'); // including service class to work with login processing
require_once('classes/CProfiles.php'); // including service class to work with profiles

$sErrors = '';
// join processing
if (! isset($_SESSION['member_id']) && $_POST['Join'] == 'Join') {
    $GLOBALS['CProfiles']->registerProfile();
}

// login system init and generation code
$sLoginForm = $GLOBALS['CLogin']->getLoginBox();

$sChat = '<h2>You do not have rights to use chat</h2>';
$sInput = $sPrivChatJs = $sRooms = '';
if ($_SESSION['member_id'] && $_SESSION['member_status'] == 'active' && $_SESSION['member_role']) {
    if ($_GET['action'] == 'update_last_nav') { // update last navigate time
        $iPid = (int)$_SESSION['member_id'];
        if ($iPid) {
            $GLOBALS['MySQL']->res("UPDATE `cs_profiles` SET `date_nav` = NOW() WHERE `id` = '{$iPid}'");
        }
        exit;
    }

    require_once('classes/CChat.php'); // including service class to work with chat

    if ($_GET['action'] == 'check_new_messages') { // check for new messages
        $iPid = (int)$_SESSION['member_id'];
        $iSender = $GLOBALS['MainChat']->getRecentMessage($iPid);

        if ($iSender) {
            $aSender = $GLOBALS['CProfiles']->getProfileInfo($iSender);
            $sName = ($aSender['first_name'] && $aSender['last_name']) ? $aSender['first_name'] . ' ' . $aSender['last_name'] : $aSender['name'];

            $oJson = new Services_JSON();
            header('Content-type: application/json');
            echo $oJson->encode(array('id' => $iSender, 'name' => $sName));
        }
        exit;
    }

    if ($_GET['action'] == 'get_private_messages') { // regular updating of messages in chat
        $sChat = $GLOBALS['MainChat']->getMessages((int)$_GET['recipient']);
        $oJson = new Services_JSON();
        header('Content-type: application/json');
        echo $oJson->encode(array('messages' => $sChat));
        exit;
    }

    $iRoom = (int)$_GET['room'];

    $sNewRoom = '';
    if ($_SESSION['member_role'] == 5) {
        $aRoomKeys = array('{room}' => $iRoom);
        $sNewRoom = strtr(file_get_contents('templates/new_room.html'), $aRoomKeys);
        if ($_POST['action'] == 'add_room' && $_POST['title'] != '') {
            $GLOBALS['MainChat']->addRoom($_POST['title']);
        }
        if ($_POST['action'] == 'delete_room' && (int)$_POST['room_id']) {
            $GLOBALS['MainChat']->deleteRoom($_POST['room_id']);
        }
    }

    // get all active rooms
    $sRooms = $GLOBALS['MainChat']->getRooms($iRoom);

    $sChatName = 'Main';
    if ($iRoom) {
        $aRoomInfo = $GLOBALS['MainChat']->getRoomInfo($iRoom);
        $sChatName = $aRoomInfo['title'];
    }

    // get last messages
    $sChat = $GLOBALS['MainChat']->getMessages(0, $iRoom);
    if ($_GET['action'] == 'get_last_messages') { // regular updating of messages in chat
        $oJson = new Services_JSON();
        header('Content-type: application/json');
        echo $oJson->encode(array('messages' => $sChat));
        exit;
    }

    // add avatar
    if ($_POST['action'] == 'add_avatar') {
        $iAvRes = $GLOBALS['CProfiles']->addAvatar();
        header('Content-Type: text/html; charset=utf-8');
        echo ($iAvRes == 1) ? '<h2 style="text-align:center">New avatar has been accepted, refresh main window to see it</h2>' : '';
        exit;
    }

    // get input form
    $sInput = $GLOBALS['MainChat']->getInputForm($iRoom);

    if ($_POST['message']) { // POST-ing of message
        $iPostRoom = (int)$_POST['room'];
        $iRes = $GLOBALS['MainChat']->acceptMessage($iPostRoom);

        $oJson = new Services_JSON();
        header('Content-type: application/json');
        echo $oJson->encode(array('result' => $iRes));
        exit;
    }

    if ($_POST['priv_message']) { // POST-ing of private messages
        $iRes = $GLOBALS['MainChat']->acceptPrivMessage();

        $oJson = new Services_JSON();
        header('Content-type: application/json');
        echo $oJson->encode(array('result' => $iRes));
        exit;
    }
    $sPrivChatJs = '<script src="js/priv_chat.js"></script>';
}

// get profiles lists
$sProfiles = $GLOBALS['CProfiles']->getProfilesBlock();
$sOnlineMembers = $GLOBALS['CProfiles']->getProfilesBlock(10, true);

// get profile avatar
$sAvatar = $GLOBALS['CProfiles']->getProfileAvatarBlock();

// draw common page
$aKeys = array(
    '{chat_name}' => $sChatName,
    '{rooms}' => $sRooms,
    '{new_room}' => $sNewRoom,
    '{form}' => $sLoginForm . $sErrors,
    '{chat}' => $sChat,
    '{input}' => $sInput,
    '{profiles}' => $sProfiles,
    '{online_members}' => $sOnlineMembers,
    '{avatar}' => $sAvatar,
    '{priv_js}' => $sPrivChatJs
);
echo strtr(file_get_contents('templates/main_page.html'), $aKeys);

Next updated file – main chat class. I have added possibility to work with different rooms here too:

classes/CChat.php

<?php

class CChat {

    // constructor
    function CChat() {}

    // add a message to database
    function acceptMessage($iPostRoom = 0) {
        $sName = $GLOBALS['MySQL']->escape($_SESSION['member_name']);
        $iPid = (int)$_SESSION['member_id'];
        $sMessage = $GLOBALS['MySQL']->escape($_POST['message']);

        if ($iPid && $sName != '' && $sMessage != '') {
            $sSQL = "
                SELECT `id`
                FROM `cs_messages`
                WHERE `sender` = '{$iPid}' AND UNIX_TIMESTAMP( ) - `when` < 5
                    AND `room` = '{$iPostRoom}'
                LIMIT 1
            ";
            $iLastId = $GLOBALS['MySQL']->getOne($sSQL);
            if ($iLastId) return 2; // as protection from very often messages

            $bRes = $GLOBALS['MySQL']->res("INSERT INTO `cs_messages` SET `sender` = '{$iPid}', `message` = '{$sMessage}', `when` = UNIX_TIMESTAMP(), `room` = '{$iPostRoom}'");
            return ($bRes) ? 1 : 3;
        }
    }

    // add a private message to database
    function acceptPrivMessage() {
        $sName = $GLOBALS['MySQL']->escape($_SESSION['member_name']);
        $iPid = (int)$_SESSION['member_id'];
        $iRecipient = (int)$_POST['recipient'];
        $sMessage = $GLOBALS['MySQL']->escape($_POST['priv_message']);

        if ($iPid && $iRecipient && $sName != '' && $sMessage != '') {
            $sSQL = "
                SELECT `id`
                FROM `cs_messages`
                WHERE `sender` = '{$iPid}' AND `recipient` = '{$iRecipient}' AND UNIX_TIMESTAMP( ) - `when` < 5
                    AND `room` = 0
                LIMIT 1
            ";
            $iLastId = $GLOBALS['MySQL']->getOne($sSQL);
            if ($iLastId) return 2; // as protection from very often messages

            $bRes = $GLOBALS['MySQL']->res("INSERT INTO `cs_messages` SET `sender` = '{$iPid}', `recipient` = '{$iRecipient}', `message` = '{$sMessage}', `when` = UNIX_TIMESTAMP()");
            return ($bRes) ? 1 : 3;
        }
    }

    // return input text form
    function getInputForm($iRoom = 0) {
        $aKeys = array(
            '{room}' => $iRoom
        );
        return strtr(file_get_contents('templates/chat.html'), $aKeys);
    }

    // get last 10 messages
    function getMessages($iRecipient = 0, $iRoom = 0) {
        $sRecipientSQL = 'WHERE `recipient` = 0';
        if ($iRecipient > 0) {
            $iPid = (int)$_SESSION['member_id'];
            $sRecipientSQL = "WHERE (`sender` = '{$iRecipient}' && `recipient` = '{$iPid}') || (`recipient` = '{$iRecipient}' && `sender` = '{$iPid}')";
        }

        $sRecipientSQL .= " AND `room` = '{$iRoom}'";

        $sSQL = "
            SELECT `a` . * , `cs_profiles`.`name`,  `cs_profiles`.`id` as 'pid' , UNIX_TIMESTAMP( ) - `a`.`when` AS 'diff'
            FROM `cs_messages` AS `a`
            INNER JOIN `cs_profiles` ON `cs_profiles`.`id` = `a`.`sender`
            {$sRecipientSQL}
            ORDER BY `a`.`id` DESC
            LIMIT 10 
        ";
        $aMessages = $GLOBALS['MySQL']->getAll($sSQL);
        asort($aMessages);

        // create list of messages
        $sMessages = '';
        foreach ($aMessages as $i => $aMessage) {
            $sExStyles = $sExJS = '';
            $iDiff = (int)$aMessage['diff'];
            if ($iDiff < 7) { // less than 7 seconds
                $sExStyles = 'style="display:none;"';
                $sExJS = "<script> $('#message_{$aMessage['id']}').fadeIn('slow'); </script>";
            }

            $sWhen = date("H:i:s", $aMessage['when']);
            $sAvatar = $GLOBALS['CProfiles']->getProfileAvatar($aMessage['pid']);
            $sMessages .= '<div class="message" id="message_'.$aMessage['id'].'" '.$sExStyles.'><b><a href="profile.php?id='.$aMessage['pid'].'" target="_blank"><img src="'. $sAvatar .'">' . $aMessage['name'] . ':</a></b> ' . $aMessage['message'] . '<span>(' . $sWhen . ')</span></div>' . $sExJS;
        }
        return $sMessages;
    }

    function getRecentMessage($iPid) {
        if ($iPid) {
            $sSQL = "
                SELECT `a` . * , `cs_profiles`.`name`,  `cs_profiles`.`id` as 'pid' , UNIX_TIMESTAMP( ) - `a`.`when` AS 'diff'
                FROM `cs_messages` AS `a`
                INNER JOIN `cs_profiles` ON `cs_profiles`.`id` = `a`.`sender`
                WHERE `recipient` = '{$iPid}' AND `room` = 0
                ORDER BY `a`.`id` DESC
                LIMIT 1
            ";

            $aMessage = $GLOBALS['MySQL']->getRow($sSQL);
            $iDiff = (int)$aMessage['diff'];
            if ($iDiff < 7) { // less than 7 seconds, = new
                return (int)$aMessage['sender'];
            }
            return;
        }
    }

    function getRandColor() {
        $aColors = array('red', 'blue', 'green', 'orange', 'indigo', 'violet');
        shuffle($aColors);
        return $aColors[0];
    }

    function getRooms($iRoom = 0) {
        $sSQL = "
            SELECT * 
            FROM `cs_rooms`
            WHERE 1
        ";
        $aRooms = $GLOBALS['MySQL']->getAll($sSQL);

        $sRooms = '';
        foreach ($aRooms as $i => $aRoom) {
            $sActive = ($iRoom == $aRoom['id']) ? ' active' : '';
            $sColor = $this->getRandColor();
            $sRooms .= '<li><a class="'.$sColor.$sActive.'" href="index.php?room='.$aRoom['id'].'">'.$aRoom['title'].'</a></li>';
        }

        $sMainActive = ($iRoom == 0) ? ' active' : '';
        $sColor = $this->getRandColor();

        return <<<EOF
<div class="roomsHolder">
    <ul class="rooms">
        <li><a class="{$sColor}{$sMainActive}" href="index.php">Main</a></li>
        {$sRooms}
    </ul>
    <div class="shadow"></div>
</div>
<div class="clear"></div>
EOF;
    }

    function getRoomInfo($i) {
        $sSQL = "
            SELECT * 
            FROM `cs_rooms`
            WHERE `id` = '{$i}'
        ";
        $aInfos = $GLOBALS['MySQL']->getAll($sSQL);
        return $aInfos[0];
    }

    function addRoom($sTitleParam) {
        $sTitle = $GLOBALS['MySQL']->escape($sTitleParam);
        $iPid = (int)$_SESSION['member_id'];

        if ($iPid && $sTitle) {
            return $GLOBALS['MySQL']->res("INSERT INTO `cs_rooms` SET `title` = '{$sTitle}', `owner` = '{$iPid}', `when` = UNIX_TIMESTAMP()");
        }
    }
    function deleteRoom($sRoomParam) {
        $iRoom = (int)$sRoomParam;

        if ($iRoom) {
            $GLOBALS['MySQL']->res("DELETE FROM `cs_messages` WHERE `room` = '{$iRoom}'");
            return $GLOBALS['MySQL']->res("DELETE FROM `cs_rooms` WHERE `id` = '{$iRoom}' LIMIT 1");
        }
    }
}

$GLOBALS['MainChat'] = new CChat();

Most of the functions (acceptMessage, acceptPrivateMessage, getInputForm, getMessages and getRecentMessages) are working properly with the rooms now. Plus I added several new functions (getRooms, getRoomInfo, addRoom and deleteRoom) to work with rooms. They are not complicated functions.

Step 5. Javascript

And, last one updated file is:

js/chat.js

$(function() {
    getMessages = function() {
        $.getJSON('index.php?action=get_last_messages&room='+iRoom, function(data) {
            if (data.messages) {
                $('.chat_messages').html(data.messages);
            }

            // get recent chat messages in loop
            setTimeout(function() {
               getMessages();
            }, 5000);
        });
    }
    getMessages();

    $('.chat_submit_form').submit(function() { 
        $.post('index.php', { message: $('.chat_submit_form input[name=message]').val(), room: $('.chat_submit_form input[name=room]').val() }, 
            function(data){
                if (data.result == 1) {
                    $('.chat_submit_form .success').fadeIn('slow', function () {
                        $(this).delay(1000).fadeOut('slow'); 
                    }); 
                } else if (data.result == 2) {
                    $('.chat_submit_form .protect').fadeIn('slow', function () {
                        $(this).delay(1000).fadeOut('slow'); 
                    }); 
                } else {
                    $('.chat_submit_form .error').fadeIn('slow', function () {
                        $(this).delay(1000).fadeOut('slow'); 
                    }); 
                }
            }
        );
        return false; 
    });

    // Update last navigation time feature
    updateLastNav = function() {
        $.getJSON('index.php?action=update_last_nav', function() {

            // refresh last nav time
            setTimeout(function(){
               updateLastNav();
            }, 180000); // 3 mins
        });
    }
    updateLastNav();
});

I modified this file in order to receive valid messages (of current active room), and to send messages to certain rooms.


Live Demo

Conclusion

I suppose that our chat script is quite ready. If you have any ideas (what will be useful to implement here) – you are always welcome to share your ideas. Don’t hesitate to contact us. Good luck and welcome back!

SIMILAR ARTICLES


99 COMMENTS

  1. plzzz help me m facing this error :(

    Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘DB_USER_NAME’@’localhost’ (using password: YES) in /home/a7829314/public_html/classes/CMySQL.php on line 19

  2. HIII
    i have tried your work and must say its excellent,,,,,
    please am having problem with the image upload portion in the join page…this is the error

    Warning: imagejpeg() [function.imagejpeg]: Unable to open ‘data/4.jpg’ for writing: No such file or directory in C:\wamp\www\ndcChats\example218\classes\CProfiles.php on line 221
    Call Stack
    # Time Memory Function Location
    1 0.0013 741984 {main}( ) ..\index.php:0
    2 0.0128 1335632 CProfiles->addAvatar( ) ..\index.php:93
    3 0.0197 1631888 imagejpeg ( )

    please help

    • Hi Rico,
      You should check if your ‘data/’ folder is writable (as it should be), because it seems that this is readable only. Check permission.

  3. hello
    i am new developer and learning
    can someone tell me how to run source file in localhost…

    thanks in advance.

    • Hi jasmeet,
      This is easy – just unpack the package on your localhost, and then – open it in your browser (from localhost)

  4. Hello , I found this tutorial is very useful to my final year project , just I want to know how to make my members that already have username and password in my database , so that they didn’t have to register for this chat.

    • Hi rizki93,
      Do you mean that you already have a table in your database with members of your another project? In this case, you can try to :
      a) modify source files of the chat to work with your table(s)
      or b) import your members into the table with members which is used by the chat

  5. Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘DB_USER_NAME’@’localhost’ (using password: YES) in C:\xampp\htdocs\example278\classes\CMySQL.php on line 19

    Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\example278\classes\CMySQL.php on line 22

    Warning: mysql_query() [function.mysql-query]: Access denied for user ‘ODBC’@’localhost’ (using password: NO) in C:\xampp\htdocs\example278\classes\CMySQL.php on line 24

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\example278\classes\CMySQL.php on line 24

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:\xampp\htdocs\example278\classes\CMySQL.php:19) in C:\xampp\htdocs\example278\classes\CLogin.php on line 7

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at C:\xampp\htdocs\example278\classes\CMySQL.php:19) in C:\xampp\htdocs\example278\classes\CLogin.php on line 7

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\example278\classes\CMySQL.php on line 45
    Database query error

    And why I’m having this problems .. please help me ..

    • Hi Rizki93,
      You might forgot to configure the chat to work with your own database in the ‘classes/CMySQL.php’ file, check it.

    • Hi Quesacotl,
      Of course this is possible, why it should be impossible? :-)
      All the necessary codes to do OAuth are presented on that website. You just need to apply it in our chat.

  6. Hi!,Admin
    when will you update this chat system ?
    and what feature will you add in the next version ?
    will you add video call, friend system, emoticon and more user groups ?
    will you make better template ?

    thanks!

    • Hi Corliansa,
      If you have any interesting ideas that could be added to the script – you can email us using the contact form at the bottom of the site.

  7. You might forgot to configure the chat to work with your own database in the ‘classes/CMySQL.php’ file, check it.
    Will you please explain it i didn’t got it.

    Thank you

    • Hello Aayush,
      Basically, you had to put your DB params in the mentioned ‘classes/CMySQL.php’ file (in the beginning of this file)

  8. Hi,
    thank you very much, Andrey , Your site is a treasure :).

    i, have a few informations on jquery, json ..!! my problem how to display a fade messages (error, succes , completing ..) in a form exemple :

    form action=insert.php id=insert
    input type=text id=fname
    input type=text id=lname
    input type=text id=date
    ….
    form

    Sending your message. Please wait…
    Thanks for sending your message! We’ll get back to you shortly.
    There was a problem sending your message. Please try again.
    Please complete all the fields in the form before sending.

    i want to use your example chat.js
    thank you ..!

    • Hi Isaac,
      Please check our js/chat.js file, we simple use fadeIn -> fadeOut to display these temporary messages

  9. hi Andrew,
    I am developing a chat system in my social network application using php, ajax, jquery and mysql. is it possible for me to add this chat system into the friend table i have created? it has fields like $id, $from, $to instead of creating new profiles is it possible to add to the existing friend table?

    • Hello Preethi,
      Yes, this is possible. If you need to use this script with another system, you just need to rewrite all the SQL queries to use it with other tables.

      • i want to use your chat application in may web site.. which is developed in dot net(c#) my web site have its own login page……. so i want only that log in to strat chat in my website. i dont want to login again, once i logged initially… is it possible??

      • Hi Nitin,
        Yes, this is possible. In your case you should integrate the chat into your project: you should make the AUTH mutual for both systems.

  10. Could you create a mod/admin only chat by adding a minimum role field to the rooms table and editing the getRooms function like this?

    function getRooms($iRoom = 0) {

    $iRole = (int)$_SESSION['member_role'];

    $sSQL = ”
    SELECT *
    FROM `cs_rooms`
    WHERE minimum_role <= $iRole
    ";

    This should work correct?

      • Thanks, i was able to get it to work. Also are there any steps I can take to speed up the time it takes for the message to be shown on the screen? And is there a way to have the message field clear every time a message is sent?

      • Hi jp277,
        Yes, of course, you just need to decrease these limits, check the CChat.php, search for ’5′ (seconds), and then for ’7′. Decrease both. You also can decrease the limit in priv_chat.js (5000ms to a less value). If you need to clean the input field – do it, with adding $(‘field’).val(”);

  11. Very Nice Andrey ! but i do have a few suggestions for this Chat , What is your email so i can email you “what i’d like to see implemented” in this chat

    • Hi Britany,
      You may contact me directly with posting your message using the ‘contact us’ form in the bottom of the website.

  12. 1)How to trim the characters in the private chat window
    2)How to remove the second name in the private chat window
    3)If we refresh the page means the private chat windows closed automatically how to rectify it.
    4)if we can add video chat option into it.
    5)How to adjust time with the system time.

    • Hello Praveen,
      Are you looking for ways to customize your instance of this chat? Yes, most of mentioned things are possible, and even implemented in our commercial product.

  13. I have been getting this message “Database query error”
    and I realise that it is from
    mysql_query(“SET names UTF8″); of Cmysql.php…

    Can you give me an example code that I should change it to?

Leave a Reply