用于 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; } } ?>