1

Topic: Simple chat / IM log viewer script

Hi folks,
I wanted to have a tool that displays the current log history of the private chat (instant messenger), depending on the user in chronological order.
So today I hacked a short and very stupid PHP script which just does ... that. It's neither perfect nor complete, only reads out the vld_chats table and displays simple HTML text. smile
Copy & paste the following code to a file like "vld_chats.php", fill in your DB data and upload it to your community server.

<?php

    $dbcfg = array( "db"=>"dbname","user"=>"dbuser","password"=>"dbpassword","host"=>"dbhost" );
    $tz = 0; /* hours, e.g. +8 */


    $dbhandle = @mysql_connect($dbcfg["host"], $dbcfg["user"], $dbcfg["password"]);
    if ($dbhandle) {
    $exists = mysql_select_db($dbcfg["db"], $dbhandle);

    echo("<HTML><HEAD><TITLE>vld_chats viewer</TITLE></HEAD><BODY>");

    if ($_GET["username"]) {
        $sql = "SELECT mid1 FROM vld_chats WHERE username = '" .$_GET["username"]. "' LIMIT 1";
        $result = mysql_query($sql, $dbhandle);
        $row = mysql_fetch_array($result, MYSQL_ASSOC);

        $sql = "SELECT message_id, username, body, timeposted, unread FROM vld_chats WHERE mid1 = '" . $row["mid1"] . "' OR mid2 = '" .  $row["mid1"] . "' ORDER BY timeposted ASC";
        $result = mysql_query($sql, $dbhandle);
        $count = mysql_num_rows($result);

        if($count) {
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $msg = $row["body"];
                if ($row["unread"]) $msg = '<I>' . $msg . '</I>';
                echo("<B>" . $row["username"] . " (" . date('Y-m-d H:i:s', $row["timeposted"]+$tz*3600) . ", #" . $row["message_id"] . "):</B><BR>" . $msg . "<BR>\n");
            }
        }
    } else {
        $sql = "SELECT distinct username FROM vld_chats ORDER BY username ASC";
        $result = mysql_query($sql, $dbhandle);
        $count = mysql_num_rows($result);

        if($count) {
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                echo("<A HREF=\"" . $_SERVER["SCRIPT_NAME"] . "?username=".$row["username"]."\">" . $row["username"] . "</A><BR>\n");
            }
        }
    }

    echo("</BODY>");
    } else {
    die ("Error: Could not connect to MySQL-Server!" . mysql_error());
    }

    mysql_close($dbhandle);
?>

It shows a list of all users that have sent a message, and when clicking on a username it shows all conversations (sent and received) top down including date/time and message id. An italic text means unread message. At the moment only the sender name is shown.

Feel free to copy or edit it ... whatever you like.

Last edited by jensrenner (2012-02-02 09:52:25)

2

Re: Simple chat / IM log viewer script

Thanks for sharing, sound good. Can you explain this in simple english  "fill in your DB data and upload it to your community server."

thanks

VPS & Dedicated Hosting Servint

3

Re: Simple chat / IM log viewer script

Ok, sorry ... I was not very precise.
Because the script must connect to the database you have to enter the details how to access the vldPersonals DB.

$dbcfg = array( "db"=>"dbname","user"=>"dbuser","password"=>"dbpassword","host"=>"dbhost" );

Instead of dbname, dbuser, dbpassword, dbhost, you have to use your actual DB account information.

Last edited by jensrenner (2012-02-02 02:27:48)

4

Re: Simple chat / IM log viewer script

thank you, how about this "Copy & paste the following code to a file like "vld_chats.php" where exactly do I install this file to my server. What is the right location to put it?

sorry I am not a coder, but this is a good idea to have.

VPS & Dedicated Hosting Servint

5

Re: Simple chat / IM log viewer script

Since the script does not contain any paths (except a reference to itself), put it anywhere you like ... e.g. in the vldPersonals root. Of course, you must be able to call the script from outside.
The database has to accept the connection from the script, but this should not be a problem as long it is on the same webserver as your community software.

Btw, I added a variable called $tz which compensates for a different server time zone. Just put the difference in hours there.

Last edited by jensrenner (2012-02-02 09:54:07)