Jump to content

Rich Internet Applications for SLA Research/FlashComm Database Interaction Start

From Wikibooks, open books for an open world

Setting Up AMFPHP

[edit | edit source]

As in the example of basic database interaction, the AMFPHP scripts from http://www.amfphp.org should be unzipped, and the gateway.php file should be placed in a directory of the web server. Also, the AMFPHP flashservices folder should be installed within the cgi-bin directory. Then, a subfolder which will be accessed by the Flash movie (for login purposes) and the FlashComm application (for saving chat text) should be created. Into this subfolder should be placed gateway.php; normally, this file should not require any editing. For this tutorial, the subfolder will be named AMFPHPChat.

In the AMFPHPChat folder, a subfolder named services should be created. The services folder will contain the PHP file that accesses the database on behalf of both the Flash movie and the FlashComm application. For this sample application, a file named DBServices.php should be created and the following code should be placed within it using a basic text editor (Notepad on Windows, vi or emacs on UNIX/LINUX, for example).

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

NEXT: FlashComm Database Interaction: Flash Movie