用於 SLA 研究的富網際網路應用/FlashComm 資料庫互動開始
外觀
與基本資料庫互動示例一樣,來自 http://www.amfphp.org 的 AMFPHP 指令碼應解壓縮,並將 gateway.php 檔案放置在 Web 伺服器的目錄中。此外,AMFPHP flashservices 資料夾應安裝在 cgi-bin 目錄中。然後,應建立一個將由 Flash 動畫(用於登入目的)和 FlashComm 應用程式(用於儲存聊天文字)訪問的子資料夾。應將 gateway.php 放置到此子資料夾中;通常,此檔案不需要任何編輯。對於本教程,子資料夾將命名為 AMFPHPChat。
在 AMFPHPChat 資料夾中,應建立一個名為 services 的子資料夾。services 資料夾將包含代表 Flash 動畫和 FlashComm 應用程式訪問資料庫的 PHP 檔案。對於此示例應用程式,應建立一個名為 DBServices.php 的檔案,並將以下程式碼放置在其中,使用基本的文字編輯器(例如 Windows 上的記事本,UNIX/LINUX 上的 vi 或 emacs)。
<?php
class DBServices
{
/** class constructor */
/** mysql var access */
var $db_host = "DATABASE SERVER ADDRESS";
var $db_name = "DATABASE NAME";
var $db_user = "USERNAME";
var $db_pwd = "PASSWORD";
// main function defined
function DBServices(){
// define the method table for defining flash remoting available methods
$this->methodTable = array();
$this->methodTable["validateUser"] = array(
"description" => "validate user in mysql table",
"access" => "remote",
);
$this->methodTable["saveChatText"] = array(
"description" => "save text chat to session",
"access" => "remote",
);
$this->methodTable["nextSession"] = array(
"description" => "get a unique(?) session identifier",
"access" => "remote",
);
$this->methodTable["destroySID"] = array(
"description" => "destroy a session identifier",
"access" => "remote",
);
}
/** check if username exists into the table */
function validateUser($uName, $pWord){
// create the connection to DB
$this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
// select the database "test"
mysql_select_db( $this->db_name );
$loginQuery = "SELECT * FROM username_pwd
WHERE username = '$uName'
AND pwd = '$pWord'";
$result = mysql_query($loginQuery);
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_array($result);
$nameMatch = $row['username'];
$UIDQuery = "SELECT * FROM username_uid WHERE username = '$nameMatch'";
$UIDResult = mysql_query($UIDQuery);
$UIDRow = mysql_fetch_array($UIDResult);
$retInfo = array();
$retInfo['username'] = $UIDRow['username'];
$retInfo['eoa'] = "endOfArray";
return $retInfo;
} else {
return "error";
}
mysql_close();
}
/** save chat text in database */
function saveChatText($theSID, $chatText) {
session_start();
// create the connection to DB
$this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
// select the database "test"
mysql_select_db( $this->db_name );
$safeText = addslashes($chatText);
$sql = "UPDATE chatText_SID
SET HTMLtext = '" . $safeText . "' "
. " WHERE session_id = '" . $theSID . "' ";
$result = mysql_query ($sql); // run the query
mysql_close();
// string returned below is for debugging purposes; it can be removed
// without any loss of functionality
return "result: " . $result . "; theSID: " . $theSID . "; session_id(): " . session_id();
}
/** get a new session */
function nextSession($user1, $user2, $timeDate) {
session_start();
// create the connection to DB
$this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
// select the database "test"
mysql_select_db( $this->db_name );
$sql = "INSERT INTO chatText_SID ( session_id, startDateTime, uName01 , uName02, HTMLtext) "
. "VALUES ( '" . session_id() . "', '$timeDate', '$user1', '$user2', )";
$result = mysql_query($sql); // run the query
mysql_close();
return session_id();
}
/** destroy session */
function destroySID(){
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), , time()-42000, '/');
}
// Finally, destroy the session.
$destruction = session_destroy();
return $destruction;
}
}
?>