You are previewing Flash® and PHP Bible.
O'Reilly logo
Flash® and PHP Bible

Book Description

The demand for rich Internet applications (RIAs) such as complete storefronts and interactive surveys is skyrocketing, as is the pressure to create these dynamic apps overnight and at low cost. This in-depth Bible provides the step-by-step instructions you need to quickly create RIAs in Flash using cost-effective, open-source PHP programming tools. You'll learn how PHP works, when you should use it in Flash, and above all, vital security techniques for keeping your interactive sites secure.

Table of Contents

  1. Copyright
  2. About the Author
  3. Credits
  4. Foreword
  5. Acknowledgments
  6. Introduction
    1. Examples and Source Files
    2. What You'll Need
  7. I. Understanding the Basics
    1. 1. Getting Started with Flash and PHP
      1. 1.1. Adding Apache to a Web Server
        1. 1.1.1. Installing Apache for Windows
          1. 1.1.1.1. Installing
          2. 1.1.1.2. Testing
        2. 1.1.2. Installing Apache for UNIX
          1. 1.1.2.1. Preinstallation
          2. 1.1.2.2. Installation
          3. 1.1.2.3. Testing
        3. 1.1.3. Modifying Apache for Windows and UNIX
      2. 1.2. Installing MySQL
        1. 1.2.1. Installing MySQL for Windows
          1. 1.2.1.1. Downloading
          2. 1.2.1.2. Installation
          3. 1.2.1.3. Testing
        2. 1.2.2. Installing MySQL for UNIX
        3. 1.2.3. Protecting MySQL
          1. 1.2.3.1. Setting a password on Windows
          2. 1.2.3.2. Setting a password on UNIX
      3. 1.3. Setting up PHP on the Web Server
        1. 1.3.1. Installing PHP for Windows
          1. 1.3.1.1. Installation
          2. 1.3.1.2. Testing
        2. 1.3.2. Installing PHP for UNIX
          1. 1.3.2.1. Building configuration parameters
          2. 1.3.2.2. Configuration
      4. 1.4. Summary
    2. 2. Exploring Flash and PHP
      1. 2.1. Introduction to the Web Server
        1. 2.1.1. Working with .htaccess files
        2. 2.1.2. Protecting your content
        3. 2.1.3. Gathering information about Apache
          1. 2.1.3.1. Starting and stopping Apache
          2. 2.1.3.2. Determining the version of Apache
          3. 2.1.3.3. Determining which modules are installed in Apache
        4. 2.1.4. Using custom error documents
          1. 2.1.4.1. Modifying Apache
          2. 2.1.4.2. Dynamic error documents
      2. 2.2. Exploring the Basics of PHP
        1. 2.2.1. Understanding variables
          1. 2.2.1.1. Strings
          2. 2.2.1.2. Booleans
          3. 2.2.1.3. Arrays
          4. 2.2.1.4. Objects
        2. 2.2.2. Working with functions
        3. 2.2.3. Understanding control structures
          1. 2.2.3.1. if
          2. 2.2.3.2. else
          3. 2.2.3.3. elseif
          4. 2.2.3.4. while
          5. 2.2.3.5. for
          6. 2.2.3.6. foreach
          7. 2.2.3.7. break
          8. 2.2.3.8. continue
          9. 2.2.3.9. switch
          10. 2.2.3.10. require
          11. 2.2.3.11. include
          12. 2.2.3.12. require_once
          13. 2.2.3.13. include_once
        4. 2.2.4. Using type checking in PHP
          1. 2.2.4.1. Type checking functions
          2. 2.2.4.2. Future version support
      3. 2.3. Exploring the Basics of MySQL
        1. 2.3.1. Using statements
          1. 2.3.1.1. SELECT
          2. 2.3.1.2. INSERT
          3. 2.3.1.3. DELETE
        2. 2.3.2. Conditions
          1. 2.3.2.1. WHERE
          2. 2.3.2.2. AND
          3. 2.3.2.3. OR
      4. 2.4. Exploring Flash
        1. 2.4.1. Flash IDE
        2. 2.4.2. Alternative editors
        3. 2.4.3. Flash-enabled devices
        4. 2.4.4. Moving forward
      5. 2.5. Summary
    3. 3. Getting Connected
      1. 3.1. Understanding Communications in Flash
        1. 3.1.1. Determining the status of PHP
        2. 3.1.2. Working with various connection types
          1. 3.1.2.1. One-way communication
          2. 3.1.2.2. Sending data to the server
          3. 3.1.2.3. Two-way communication
      2. 3.2. Connecting Flash to PHP
        1. 3.2.1.
          1. 3.2.1.1. Concatenating data
          2. 3.2.1.2. Multiple pieces of data
      3. 3.3. Connecting PHP to MySQL
        1. 3.3.1. Determining the status of MySQL
        2. 3.3.2. Connecting to MySQL
          1. 3.3.2.1. Persistent connection
          2. 3.3.2.2. Closing the connection
          3. 3.3.2.3. Selecting the database
        3. 3.3.3. Bringing it all together
          1. 3.3.3.1. Connecting the PHP
          2. 3.3.3.2. Building the Flash
      4. 3.4. Summary
    4. 4. Working with Data
      1. 4.1. Loading Data in Flash
        1. 4.1.1. Understanding the classes used to load data
          1. 4.1.1.1. URLRequest
          2. 4.1.1.2. URLLoader
          3. 4.1.1.3. Set up the callback
        2. 4.1.2. Putting it all together
          1. 4.1.2.1. Assigning multiple events
      2. 4.2. Handling Loaded Data
        1. 4.2.1. One-way loading
        2. 4.2.2. Two-way loading
      3. 4.3. Loading XML in Flash
        1. 4.3.1.
          1. 4.3.1.1. Using E4X with XML
      4. 4.4. Working with XML in PHP
        1. 4.4.1. Loading XML
          1. 4.4.1.1. Old-fashioned Dom XML
          2. 4.4.1.2. Simple XML
        2. 4.4.2. Sending XML
          1. 4.4.2.1. Printing dynamic XML
          2. 4.4.2.2. Dynamic XML from the database
      5. 4.5. Loading Images Using PHP
        1. 4.5.1. Setting up the image loader
      6. 4.6. Summary
    5. 5. Interacting with the User
      1. 5.1. Form Development Using Flash
      2. 5.2. Creating a Contact Form
        1. 5.2.1. Calling the PHP
        2. 5.2.2. Contact form event handlers
        3. 5.2.3. Mailing in PHP
          1. 5.2.3.1. Globals
          2. 5.2.3.2. Keep security in mind
      3. 5.3. Login Module in Flash
        1. 5.3.1. Code skeleton
        2. 5.3.2. Login event handlers
        3. 5.3.3. Server integration for login module
      4. 5.4. Summary
    6. 6. Working with Cookies
      1. 6.1. Loading Cookies
        1. 6.1.1. Using cookies with PHP
          1. 6.1.1.1. Loading a cookie
          2. 6.1.1.2. Saving a cookie
          3. 6.1.1.3. Cookie expiration
          4. 6.1.1.4. Deleting a cookie
          5. 6.1.1.5. Assigning multiple cookies
        2. 6.1.2. Using cookies in Flash
          1. 6.1.2.1. Loading shared objects
          2. 6.1.2.2. Saving shared objects
          3. 6.1.2.3. Deleting a shared object
          4. 6.1.2.4. Bringing it all together
        3. 6.1.3. Discovering the benefits of using PHP cookies
      2. 6.2. Summary
  8. II. Developing Interactive Content
    1. 7. Maintaining Security while Working with User Input
      1. 7.1. Using Caution with User Input
        1. 7.1.1. Safely handling file uploads
          1. 7.1.1.1. Checking for valid file extensions
          2. 7.1.1.2. Using a better way to check for file extensions
        2. 7.1.2. Checking for valid input
          1. 7.1.2.1. Validating ZIP codes
          2. 7.1.2.2. Looking for valid types
      2. 7.2. Cleaning User Data
        1. 7.2.1. Sanitizing the data
          1. 7.2.1.1. Creating safe SQL queries
        2. 7.2.2. Properly cleaning HTML data
      3. 7.3. Storing Data
        1. 7.3.1. Securely writing to a file
          1. 7.3.1.1. Working with data in a logical order
          2. 7.3.1.2. Creating a class to handle sanitization
          3. 7.3.1.3. Working with magic_quotes options
          4. 7.3.1.4. Disabling magic_quotes
          5. 7.3.1.5. Using shared objects
          6. 7.3.1.6. Using PHP to handle data from shared objects
        2. 7.3.2. Creating and storing a safe password using PHP
      4. 7.4. Returning Data
        1. 7.4.1. Securely returning data
        2. 7.4.2. Using a more secure approach for returning data
          1. 7.4.2.1. Using unique responses
          2. 7.4.2.2. Securing your files
      5. 7.5. Understanding the Flash Security Sandbox
        1. 7.5.1. Setting the sandbox type
        2. 7.5.2. Using the sand box Type property
        3. 7.5.3. Determining the active sandbox
          1. 7.5.3.1. Running applications in the local sandbox
          2. 7.5.3.2. Unloading an application
        4. 7.5.4. Ensuring an application cannot be shared
      6. 7.6. Summary
    2. 8. Using Sockets
      1. 8.1. Understanding Sockets
        1. 8.1.1. Security in sockets
        2. 8.1.2. Implementing a socket server
        3. 8.1.3. Understanding the socket connection
          1. 8.1.3.1. Parts of a socket
          2. 8.1.3.2. Binding
          3. 8.1.3.3. Listening
      2. 8.2. Working with Sockets in PHP
        1. 8.2.1. Looking for command-line version of PHP
        2. 8.2.2. Building a socket server
          1. 8.2.2.1. Simple PHP-based socket server
          2. 8.2.2.2. Responding to the client connection
        3. 8.2.3. Testing the socket server
        4. 8.2.4. Creating a persistent socket server
          1. 8.2.4.1. Recursive loops
          2. 8.2.4.2. Managing multiple communications
      3. 8.3. Working with Sockets in Flash
        1. 8.3.1. Initializing a socket connection
        2. 8.3.2. Event handlers
          1. 8.3.2.1. Special handlers
        3. 8.3.3. Remote socket connections
          1. 8.3.3.1. Security trust
          2. 8.3.3.2. Loading the policy file
        4. 8.3.4. Using a class for socket connections
          1. 8.3.4.1. Basic socket class
          2. 8.3.4.2. Using the SimpleSocket class
      4. 8.4. Building a Chat Client with Sockets Using Flash and PHP
        1. 8.4.1. PHP socket server for the chat client
          1. 8.4.1.1. Chat properties
          2. 8.4.1.2. Excluding the master server from communication
          3. 8.4.1.3. Special chat parameters
          4. 8.4.1.4. Master client connection
          5. 8.4.1.5. Notifying a specific client
          6. 8.4.1.6. Notifying all clients
          7. 8.4.1.7. Handling errors
          8. 8.4.1.8. Ending a connection
          9. 8.4.1.9. Server monitoring
        2. 8.4.2. Connecting to the socket server
          1. 8.4.2.1. Building the connection file
          2. 8.4.2.2. Testing the connection
        3. 8.4.3. Building the Flash client
          1. 8.4.3.1. Event handler functions
          2. 8.4.3.2. Trapping key presses
          3. 8.4.3.3. Submit message handler
          4. 8.4.3.4. Clearing the message input
          5. 8.4.3.5. Checking the text input length
          6. 8.4.3.6. Maintaining a stable interface
          7. 8.4.3.7. Sending the initial request
          8. 8.4.3.8. Sending messages to the socket server
          9. 8.4.3.9. Handling status updates
      5. 8.5. Summary
  9. III. Extending Flash and PHP
    1. 9. Working with Third-Party Libraries
      1. 9.1. Going over Third-Party Libraries
        1. 9.1.1. Other types of third-party libraries
          1. 9.1.1.1. Commercial Flash libraries
          2. 9.1.1.2. Open source Flash libraries
        2. 9.1.2. Libraries in PHP
      2. 9.2. Installing Third-Party Libraries
        1. 9.2.1. Installing libraries in Flash CS3
          1. 9.2.1.1. Installing as an MXP file
          2. 9.2.1.2. Installing as a standard class package
            1. 9.2.1.2.1. Creating a custom classpath
            2. 9.2.1.2.2. Default classpaths
        2. 9.2.2. Installing libraries in PHP
      3. 9.3. Using Third-Party Libraries
        1. 9.3.1. Working with libraries in Flash CS3
        2. 9.3.2. Working with Libraries in PHP
          1. 9.3.2.1. Testing the MySQL library
          2. 9.3.2.2. Installing the MySQL library
      4. 9.4. Glancing at AMFPHP
        1. 9.4.1. AMFPHP for AS3 and PHP Developers
          1. 9.4.1.1. AMFPHP and ActionScript 3
          2. 9.4.1.2. Installing AMFPHP
          3. 9.4.1.3. Testing the installation
          4. 9.4.1.4. AMFPHP debugging tools
          5. 9.4.1.5. AMFPHP service browser
          6. 9.4.1.6. AMFPHP debug gateway
        2. 9.4.2. Testing AMFPHP with a custom service
        3. 9.4.3. Using AMFPHP in Flash
          1. 9.4.3.1. Loading the remoting classes
      5. 9.5. Building a Real-World Application Using AMFPHP
        1. 9.5.1. AMFPHP services
        2. 9.5.2. ActionScript for AMFPHP integration
          1. 9.5.2.1. Clearing old results
      6. 9.6. Summary
    2. 10. Using Object-Oriented Programming
      1. 10.1. Understanding OOP
        1. 10.1.1. Overview of OOP practices
          1. 10.1.1.1. Classes
          2. 10.1.1.2. Constructor
          3. 10.1.1.3. Packages
          4. 10.1.1.4. Importing
          5. 10.1.1.5. Static methods and properties
          6. 10.1.1.6. Singletons
          7. 10.1.1.7. Getters and setters
      2. 10.2. Using Classes in PHP
        1. 10.2.1. Importing classes in PHP
        2. 10.2.2. Instantiation
        3. 10.2.3. Multiple classes
      3. 10.3. Using Classes in Flash
        1. 10.3.1. Importing
        2. 10.3.2. Document class
        3. 10.3.3. Library classes
      4. 10.4. Using Flash and PHP to Build Custom Classes
      5. 10.5. Summary
  10. IV. Developing Applications
    1. 11. Developing Basic Applications
      1. 11.1. Understanding Elements of an Application
        1. 11.1.1. Understanding application design
          1. 11.1.1.1. Working with pseudo-code
        2. 11.1.2. Finalizing the planning stage
      2. 11.2. Developing a Chat Client
        1. 11.2.1. The Flash portion
          1. 11.2.1.1. Stop caching with dynamic data
          2. 11.2.1.2. Handling XML response
        2. 11.2.2. PHP for chat application
          1. 11.2.2.1. Using PHP to connect to MySQL
          2. 11.2.2.2. Creating a database table
      3. 11.3. Using PHP to Develop a Photo Gallery
        1. 11.3.1. Developing the Action Script
          1. 11.3.1.1. Anonymous functions
        2. 11.3.2. Photo gallery navigation
          1. 11.3.2.1. Image navigation
        3. 11.3.3. PHP for the photo gallery
      4. 11.4. Using PHP to Develop an RSS Reader
        1. 11.4.1. Importing classes
        2. 11.4.2. Loading the PHP
      5. 11.5. Using PHP, Flash, and MySQL to Develop a Dynamic Banner Ad
        1. 11.5.1. Opening a browser window
        2. 11.5.2. Developing the PHP
        3. 11.5.3. Random selection
      6. 11.6. Using PHP to Develop a Hit Counter
        1. 11.6.1. Hit counter logic
        2. 11.6.2. Developing the Flash hit counter
      7. 11.7. Summary
    2. 12. Developing Real-World Applications
      1. 12.1. Understanding Real-World Applications
      2. 12.2. Using PayPal in Flash
        1. 12.2.1.
          1. 12.2.1.1. Signing up for PayPal Premier
        2. 12.2.2. Using POST data
        3. 12.2.3. Using sendToURL
        4. 12.2.4. Setting up PayPal communication
      3. 12.3. Using Flash and PHP to Build a Cart
        1. 12.3.1. Designing the shopping cart
          1. 12.3.1.1. Developing the StoreItem class
          2. 12.3.1.2. Developing the ShoppingCartItem class
        2. 12.3.2. Building the PHP
          1. 12.3.2.1. ShoppingCart
          2. 12.3.2.2. ShoppingCartItem
          3. 12.3.2.3. StoreItem
          4. 12.3.2.4. Timeline code
          5. 12.3.2.5. PHP code
      4. 12.4. Using PHP and Flash to Build an Amazon Search Application
        1. 12.4.1. Using the Amazon Web Service
        2. 12.4.2. Simplifying the XML response
      5. 12.5. Developing a Photo Gallery Using flickr
        1. 12.5.1. Interfacing with the Web service
        2. 12.5.2. Building the custom XML document
      6. 12.6. Summary
    3. 13. Using Advanced Real-World Applications
      1. 13.1. Building a Drawing Application in Flash
        1. 13.1.1. Drawing API in Flash
      2. 13.2. Using GD Library in PHP
        1. 13.2.1. Generating an image in the GD library
        2. 13.2.2. Gathering the pixel data in Flash
      3. 13.3. Using Flash to Develop a Site Monitor
        1. 13.3.1. Developing the PHP for the site monitor
        2. 13.3.2. Using PHP to e-mail the administrator
        3. 13.3.3. Developing the ActionScript for the site monitor
      4. 13.4. Using Flash to Develop a Video Player
      5. 13.5. Developing a Poll Application
        1. 13.5.1. Building the PHP and MySQL
        2. 13.5.2. Developing the ActionScript for the poll
      6. 13.6. Building a Simple File Editor
      7. 13.7. Summary
    4. 14. Debugging Applications
      1. 14.1. Using Error Reporting in PHP
        1. 14.1.1. Displaying errors for debugging
          1. 14.1.1.1. Debugging variables
          2. 14.1.1.2. Debugging arrays
        2. 14.1.2. Understanding the error levels
      2. 14.2. Debugging in Flash
      3. 14.3. Using an Alternative Trace
      4. 14.4. Summary
  11. V. Server, Application, and Database Maintenance
    1. 15. Maintaining an Application
      1. 15.1. Commenting Code
        1. 15.1.1. Understanding styles for commenting code
          1. 15.1.1.1. Block comments
          2. 15.1.1.2. Line comments in MySQL
          3. 15.1.1.3. Inline comments
          4. 15.1.1.4. Compiler comments
          5. 15.1.1.5. Comments in XML
          6. 15.1.1.6. Comments in Apache
          7. 15.1.1.7. Mixing comments
        2. 15.1.2. Removing comments and debug helpers
      2. 15.2. Managing a ChangeLog
        1. 15.2.1. Bug tracking
          1. 15.2.1.1. Using bug tracking with teams
        2. 15.2.2. Additional uses
        3. 15.2.3. Dynamic creation of changes
      3. 15.3. Managing Multiple Versions
        1. 15.3.1. Version control applications
        2. 15.3.2. Version control support in CS3
          1. 15.3.2.1. Using Version Cue in Flash CS3
        3. 15.3.3. Setting up version control
          1. 15.3.3.1. Using version control
          2. 15.3.3.2. Backing up version control
          3. 15.3.3.3. Additional use of version control
      4. 15.4. Using Custom Libraries
        1. 15.4.1. Using custom libraries with version control
        2. 15.4.2. Publishing an SWC
      5. 15.5. Summary
    2. 16. Maintaining a Scalable and More Efficient Server
      1. 16.1. Running an Updated Server
        1. 16.1.1. Using automatic updates
        2. 16.1.2. Zend Platform
      2. 16.2. Working with Development Installs
        1. 16.2.1. Building another version of Apache on the same system
        2. 16.2.2. Working with bleeding-edge technology
        3. 16.2.3. Dependencies
      3. 16.3. Caching and Optimizing
        1. 16.3.1. Optimizing PHP
          1. 16.3.1.1. Processor versus RAM
          2. 16.3.1.2. Ten tips for optimizing your code
          3. 16.3.1.3. Zend Optimizer
          4. 16.3.1.4. Zend Studio
        2. 16.3.2. Optimizing Apache
          1. 16.3.2.1. Hardware
          2. 16.3.2.2. Dedicated server
          3. 16.3.2.3. Configuration files
          4. 16.3.2.4. Logging and errors
          5. 16.3.2.5. MaxClients
        3. 16.3.3. Optimizing MySQL
          1. 16.3.3.1. Better performance from installation
          2. 16.3.3.2. Logging slow queries
          3. 16.3.3.3. Checking tables
          4. 16.3.3.4. Limit startup options
          5. 16.3.3.5. Additional tools
        4. 16.3.4. Caching
          1. 16.3.4.1. Working with memcached
        5. 16.3.5. Installing memcached on Linux
          1. 16.3.5.1. Installing libevent
          2. 16.3.5.2. Installing memcached
          3. 16.3.5.3. Downloading the memcached extension for PHP
          4. 16.3.5.4. Installing the memcached PHP extension
        6. 16.3.6. Installing memcached on Windows
        7. 16.3.7. Wrapping up installation for Linux and Windows
          1. 16.3.7.1. Saving data to the cache
          2. 16.3.7.2. Loading cached data
        8. 16.3.8. Managing servers
          1. 16.3.8.1. Adding new servers
          2. 16.3.8.2. Closing a connection
          3. 16.3.8.3. Deleting an item in the cache
          4. 16.3.8.4. Moving forward with memcached
      4. 16.4. Handling Backups
        1. 16.4.1. File management
          1. 16.4.1.1. Managing necessary files
          2. 16.4.1.2. Limit larger files
        2. 16.4.2. Backup management
          1. 16.4.2.1. Timed backups
          2. 16.4.2.2. Backup directories using PHP
        3. 16.4.3. Using PHP to back up databases
      5. 16.5. Summary
    3. 17. Building Complete Advanced Applications
      1. 17.1. Building a Basic Video Player
      2. 17.2. Building a Video Player in Flash and PHP
        1. 17.2.1. Getting started
          1. 17.2.1.1. Building the database and MySQL tables
          2. 17.2.1.2. Adding sample data to the tables
          3. 17.2.1.3. Testing the database
        2. 17.2.2. Remoting integration
          1. 17.2.2.1. Database connector class
          2. 17.2.2.2. Video class
          3. 17.2.2.3. Testing the classes
        3. 17.2.3. Advanced video player development
          1. 17.2.3.1. Creating the document class
          2. 17.2.3.2. Video Player constructor
          3. 17.2.3.3. get Categories method
          4. 17.2.3.4. category Response Handler method
          5. 17.2.3.5. category Handler method
          6. 17.2.3.6. getVideos method
          7. 17.2.3.7. videosResponseHandler method
          8. 17.2.3.8. listltemHandler method
          9. 17.2.3.9. Handling errors in remoting responses
        4. 17.2.4. Building the VideoListItem class
          1. 17.2.4.1. VideoListltem constructor
          2. 17.2.4.2. setItem method
          3. 17.2.4.3. Mouse handler methods
          4. 17.2.4.4. Defining the getter and setter methods
      3. 17.3. Working with Video Tracking
        1. 17.3.1. Updating the video class
          1. 17.3.1.1. Checking for valid records
          2. 17.3.1.2. Adding video tracking to ActionScript
        2. 17.3.2. updateVideoTracking method
      4. 17.4. Building a User Login Component in Flash
        1. 17.4.1. Developing the LoginWindow class
          1. 17.4.1.1. LoginWindow constructor
          2. 17.4.1.2. redraw method
          3. 17.4.1.3. Swapping placement of two MovieClips
          4. 17.4.1.4. Centering a MovieClip
          5. 17.4.1.5. close method
          6. 17.4.1.6. resetHandler method
          7. 17.4.1.7. loginHandler method
        2. 17.4.2. Testing the login component
          1. 17.4.2.1. Assigning a stage event handler
          2. 17.4.2.2. Handling the login attempt
        3. 17.4.3. Building the PHP login manager class
          1. 17.4.3.1. Creating the user's MySQL table
          2. 17.4.3.2. Building the user class
          3. 17.4.3.3. login method
        4. 17.4.4. Adding remoting to the login component
          1. 17.4.4.1. loginHandler method
          2. 17.4.4.2. Handling the login response
          3. 17.4.4.3. Handling response errors
      5. 17.5. Finalizing the Video Player
        1. 17.5.1. Using an external library
        2. 17.5.2. Adding the login component
      6. 17.6. Summary