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. ![]()
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)