[分享] 《魔镜在线》一个在线随机聊天网站源码
程序使用PHP+MySQL,jQuery实现Ajax,页面布局用的DIV+CSS,并有一个VB写的客户端程序。
原演示地址:http://ask.n9sky.com/MagicMirror/ (现在不能访问了)
现在贴出部分代码:
server.php 主要的操作处理都在这个文件
<?php
$do = @$_GET['do'];
if ('' == $do)
{
exit('Missing Action');
}
require 'config.php';
require 'dll.php';
session_start();
// check session
if ('' == $_SESSION['sid']) exit('登录超时,请关闭重试!');
switch ($do)
{
case 'init':
// check user && update
dll::import('mysql');
$db = new mysql();
$db->connect($dbs['server'], $dbs['user'], $dbs['password']);
$db->database($dbs['database']);
// update
$rs = $db->update(DBPrefix.'user', 'sid="'.$_SESSION['sid'].'"');
$rs->set('client_to', '');
$rs->set('role', RoleOnline);
$rs->set('last_access_time', time());
$rs->setfunc('login_count', 'login_count+1');
$rs->set('last_login_time', time());
$ip = $_SERVER['REMOTE_ADDR'];
$ipx = explode('.', $ip);
$iplong =
$ipx[0] * 16777216 +
$ipx[1] * 65536 +
$ipx[2] * 256 +
$ipx[3];
$rs->set('last_login_ip', $iplong);
if (!$rs->update())
{
// add new
$rs = $db->insert(DBPrefix.'user');
$rs->set('sid', $_SESSION['sid']);
$rs->set('client_to', '');
$rs->set('role', RoleOnline);
$rs->set('last_access_time', time());
$rs->set('login_count', 1);
$rs->set('last_login_time', time());
$rs->set('last_login_ip', $iplong);
$rs->insert();
}
$db->close();
// display init page
display('init.html');
break;
case 'status':
dll::import('mysql');
// access $dbs
global $dbs;
$db = new mysql();
$db->connect($dbs['server'], $dbs['user'], $dbs['password']);
$db->database($dbs['database']);
// update access
update_access($db);
// import mysql.class
display('server.status.html');
$db->close();
break;
case 'work':
$mode = $_GET['mode'];
// update work mode
dll::import('mysql');
$db = new mysql();
$db->connect($dbs['server'], $dbs['user'], $dbs['password']);
$db->database($dbs['database']);
// update
$rs = $db->update(DBPrefix.'user', 'sid="'.$_SESSION['sid'].'"');
switch ($mode)
{
case 'answer':
$rs->set('role', RoleAnswer);
break;
case 'asker':
$rs->set('role', RoleAsker);
break;
case 'chater':
$rs->set('role', RoleChater);
break;
case 'offline':
$rs->set('role', RoleOffline);
$rs->set('client_to', '');
// delete input status cache
@unlink(dirname('.').'/cache/'.$_SESSION['sid'].'.input.status');
// clear session
unset($_SESSION);
break;
}
$rs->set('last_access_time', time());
$rs->update();
$db->close();
// display mode page
if ($mode != 'offline')
{
display('work.'.$mode.'.html');
}
else
{
echo 'logout';
}
break;
case 'looking':
$mode = $_GET['mode'];
// finding target
dll::import('mysql');
$db = new mysql();
$db->connect($dbs['server'], $dbs['user'], $dbs['password']);
$db->database($dbs['database']);
// check my client
$result = $db->getRow('select client_to from '.DBPrefix.'user where sid="'.$_SESSION['sid'].'"');
$findout = false;
$passive_client = false;
if ($result[0] == "")
{
// finding new
switch ($mode)
{
case 'answer':
// looking for asker
$result = $db->getRow('select sid from '.DBPrefix.'user where client_to="" and sid <>"'.$_SESSION['sid'].'" and role='.RoleAsker);
if (!$result)
{
echo 'notfind';
}
else
{
$_SESSION['target'] = $result[0];
$findout = true;
echo 'findout';
}
break;
case 'asker':
// looking for answer
$result = $db->getRow('select sid from '.DBPrefix.'user where client_to="" and sid <>"'.$_SESSION['sid'].'" and role='.RoleAnswer);
if (!$result)
{
echo 'notfind';
}
else
{
$_SESSION['target'] = $result[0];
$findout = true;
echo 'findout';
}
break;
case 'chater':
// looking for chater
$result = $db->getRow('select sid from '.DBPrefix.'user where client_to="" and sid <>"'.$_SESSION['sid'].'" and role='.RoleChater);
if (!$result)
{
echo 'notfind';
}
else
{
$_SESSION['target'] = $result[0];
$findout = true;
echo 'findout';
}
break;
}
}
else
{
// return findout
$_SESSION['target'] = $result[0];
$findout = true;
$passive_client = true;
echo 'findout';
}
if ($findout)
{
// update client
$db->exec('update '.DBPrefix.'user set client_to="'.$_SESSION['target'].'" where sid="'.$_SESSION['sid'].'"');
// update target client
$db->exec('update '.DBPrefix.'user set client_to="'.$_SESSION['sid'].'" where sid="'.$_SESSION['target'].'"');
// delete my last message
$db->delete(DBPrefix.'message', 'sid="'.$_SESSION['sid'].'"');
if (!$passive_client)
{
// delete target last message
$db->delete(DBPrefix.'message', 'sid="'.$_SESSION['target'].'"');
}
}
$db->close();
break;
case 'input':
$status = $_GET['status'];
@file_put_contents(dirname('.').'/cache/'.$_SESSION['sid'].'.input.status', $status);
break;
case 'sender':
$channel = $_GET['channel'];
// looking target
dll::import('mysql');
$db = new mysql();
$db->connect($dbs['server'], $dbs['user'], $dbs['password']);
$db->database($dbs['database']);
if ('upload' == $channel)
{
$_POST['content'] = htmlspecialchars(urldecode($_POST['content']));
$rs = $db->insert(DBPrefix.'message');
$rs->set('sid', $_SESSION['sid']);
$rs->set('content', $_POST['content']);
$rs->set('submit_time', time());
$rs->insert();
echo 'ok';
}
elseif ('download' == $channel)
{
// check target client
$result = $db->getRow('select * from '.DBPrefix.'user where sid="'.$_SESSION['target'].'"');
if ($result['client_to'] != $_SESSION['sid'])
{
$db->exec('update '.DBPrefix.'user set client_to="" where sid="'.$_SESSION['sid'].'"');
json_message_echo('offline','From ['.$_SESSION['target'].']');
}
else
{
$lastMsgTime = (int)$_GET['lastMsgTime'];
if ($lastMsgTime == 0)
{
$rs = $db->query('select * from '.DBPrefix.'message where sid="'.$_SESSION['target'].'"');
}
else
{
$rs = $db->query('select * from '.DBPrefix.'message where sid="'.$_SESSION['target'].'" and submit_time>'.$lastMsgTime.'');
}
if ($rs->count == 0)
{
json_message_echo('input', @file_get_contents(dirname('.').'/cache/'.$_SESSION['target'].'.input.status'));
}
else
{
echo '[';
for ($i=0; $i <$rs->count; $i++)
{
json_message_echo('message', $rs->row['submit_time'].':'.$rs->row['content']);
if (($i+1) < $rs->count) echo ',';
$rs->moveNext();
}
echo ']';
}
}
}
$db->close();
break;
default:
exit('UnSupport Action');
break;
}
?>
Option ExplicitPrivate Server As StringPrivate confirmQuit As BooleanPrivate Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As LongPrivate Sub Form_Load() '服务器地址 Server = "ask.n9sky.com" '获取序列号 Dim ComID As String ComID = Disk_Info("D") '获取D盘序列号 If ComID = "" Then ComID = Disk_Info("C") 'D盘错误,继续获取C盘序列号 End If '跳转地址 Web.Navigate "http://" & Server & "/MagicMirror/load.php?me=" & ComIDEnd SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) If confirmQuit Then End End If Dim Click As Integer If UnloadMode = 0 Then Click = MsgBox("您确认要退出魔镜吗?点击取消强制关闭程序", vbYesNoCancel) Else Click = 6 End If If Click = 6 Then Cancel = 1 Web.Navigate "javascript:session_logout();" ElseIf Click = 7 Then Cancel = 1 ElseIf Click = 2 Then End End IfEnd SubPrivate Sub Web_TitleChange(ByVal Text As String) If Text = "MMI_EXIT" Then confirmQuit = True Unload Me End If If Left(Text, 7) <> "http://" Then Me.Caption = Text End IfEnd SubPrivate Function Disk_Info(ByVal iDriver As String) As String Dim Driver, VolName, FSys As String Dim volNumber, MCM, FSF As Long Dim Res As Long Driver = iDriver & ":\" Res = GetVolumeInformation(Driver, VolName, 127, volNumber, MCM, FSF, FSys, 127) If Res = 1 Then Disk_Info = Hex(MCM) & Hex(FSF) & Hex(volNumber) Else Disk_Info = "" End IfEnd Function