Makestation

Full Version: "Who is online" script for Ajax chat (can be integrated universally)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a very simple script that generates a list of users online in Ajax Chat, and it can be integrated with your website easily using an iframe. You can use notepad++ to paste this into a file named online.php, or you can alternatively download the attached file. You will need to manually configure the database connection settings (on line 2 of the script) but other than that it should work without any need for changes. It also puts the user's username in italics if [Away] is found in the username, which is designed to make it compatible with "AFK" command modifications. You can modify this as needed.

Code:
<?php
$con=mysqli_connect('DB_host','DB_user','DB_password','DB_name');


$sql = 'SELECT
userID,
userName
FROM
`ajax_chat_online`;';
$result = mysqli_query($con, $sql);
echo "Users Online: ";
$i=0;
while($row = mysqli_fetch_array($result))
{
$away_text = "[Away]"; // This will italicized usernames that have [Away] anywhere in the username. This can be modified as needed.
if (strrpos ($row['userName'], $away_text, -1)){
$string = str_replace("[Away]", "", $row['userName']);
echo "<i>".$string. "</i>";
}
else {
echo $row['userName'];
}
echo ", ";
$i++;
}
if ($i != 1) {
echo " <b>".$i." members online</b>";
}
else {
echo " <b>".$i." member online</b>";
}

mysqli_close($con);
?>
Important: : Make sure to use a program such as notepad++ to paste the contents of this file. Otherwise, extra space will be put at the beginning of the file that will make it unexecutable. You may also download the attached file.

I have set this up to be integrated on the index using an iframe at several websites. You can use something like this to integrate this script into any place on your forum or website. Modify this as needed.

Code:
<iframe style="display:inline;" src="http://example.com/chat/online.php" height="44px" marginheight="1px" frameborder="0" width="550px" scrolling="auto" seamless>Live Chat <a href="http://example.com/online.php">click here to see who is online. </a></iframe>