I ran into a few snags, the biggest of which being that you can't utilize separate pages in the chat. The chat room must be called from the same page otherwise hashing gets messed up and they just get redirected to whatever page was used first.
Without further ado, here's the code:
index.php, this file checks if there are any admins online and directs users to a contact page or to the chat. Most likely this code will be pulled out and placed prominently on our main page with a pretty button or something.
- Code: Select all
<?php
require_once dirname(__FILE__)."/src/pfcinfo.class.php";
session_start();
$info = new pfcInfo( md5("Support Chat") );
// NULL is used to get all the connected users, but you can specify
// a channel name to get only the connected user on a specific channel
$users = $info->getOnlineNick(NULL);
$nb_users = count($users);
echo '<div style="margin: auto; width: 70%; border: 1px solid red; background-color: #FDD; padding: 1em;">';
$admin_online = 0;
foreach($users as $u) {
if($u == 'ataylor' || $u == 'mwilson') {
$nb_users--;
$admin_online = 1;
}
}
if($admin_online) {
$_SESSION['users'] = $nb_users + 1;
echo 'Admin online, please join <a href="chat.php">chat</a>';
} else {
echo 'Sorry support is closed right now. Please try contacting us.';
}
echo "</div>";
?>
The one thing special about this page is that it sets a session variable with the number of non-admin users +1. This number is used to dynamically assign names and chat rooms to our guests so they are in separate rooms.
Next up is passwords.php. This just holds our passwords.
- Code: Select all
<?php
$USERS["ataylor"] = "password";
$USERS["mwilson"] = "password";
//$USERS["username3"] = "password3";
?>
Next we have admin.php.. a very simple login form for admins.
- Code: Select all
<?php
session_start();
include("passwords.php");
if ($_POST["ac"]=="log") { /// do after login form is submitted
if ($USERS[$_POST["username"]]==$_POST["password"]) { /// check if submitted username and password exist in $USERS$
$_SESSION["logged"]=$_POST["username"];
} else {
echo 'Incorrect username/password. Please, try again.';
};
};
if (array_key_exists($_SESSION["logged"],$USERS)) { //// check if user is logged or not
//// if user is logged show a message
echo 'Logged in as admin, please join the <a href="chat.php">support room</a>.';
} else { //// if not logged show login form
echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> ';
echo 'Username: <input type="text" name="username" /><br />';
echo 'Password: <input type="password" name="password" /><br />';
echo '<input type="submit" value="Login" />';
echo '</form>';
};
?>
Finally we have the chat room itself. chat.php
- Code: Select all
<?php
session_start();
include('passwords.php');
if(!array_key_exists($_SESSION['logged'],$USERS)) {
//user not logged in
require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Support chat";
$params["nick"] = "guest".$_SESSION['users']; // setup the intitial nickname
//$params['timeout'] = 15;
//$params['frozen_nick'] = true;
$params['max_msg'] = 0;
$params['channels'] = array("Support".$_SESSION['users']);
//$params["isadmin"] = true; // do not use it on production servers ;)
$params['admins'] = array('ataylor' => 'fnord','mwilson' => 'dwcpw00');
$params["serverid"] = md5("Support Chat");
//$params["serverid"] = md5(__FILE__); // calculate a unique id for this chat
//$params["debug"] = true;
$chat = new phpFreeChat( $params );
} else {
require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Support chat";
$params['nick'] = $_SESSION['logged'];
//$params["nick"] = "guest".rand(1,1000); // setup the intitial nickname
//$params['timeout'] = 15;
//$params['frozen_nick'] = true;
$params['max_msg'] = 0;
$params['channels'] = array('Support1','Support2','Support3');
$params["isadmin"] = true; // do not use it on production servers ;)
//$params['admins'] = array('ataylor' => 'fnord','mwilson' => 'dwcpw00');
$params["serverid"] = md5("Support Chat");
//$params["serverid"] = md5(__FILE__); // calculate a unique id for this chat
//$params["debug"] = true;
$chat = new phpFreeChat( $params );
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>phpFreeChat- Sources Index</title>
<!-- <link rel="stylesheet" title="classic" type="text/css" href="style/generic.css" /> -->
<!-- <link rel="stylesheet" title="classic" type="text/css" href="style/header.css" /> -->
<!-- <link rel="stylesheet" title="classic" type="text/css" href="style/footer.css" /> -->
<!-- <link rel="stylesheet" title="classic" type="text/css" href="style/menu.css" /> -->
<link rel="stylesheet" title="classic" type="text/css" href="style/content.css" />
</head>
<body>
<div class="content">
<?php $chat->printChat(); ?>
</div>
</body></html>
Admins go to admin.php first to login. Then they go to chat.php which automatically assigns them admin rights and puts them in the first three support rooms.
Hope it's helpful.
Anthony