Table of contents:

Sessions

PHP Sessions are an important part of why anyone would use AMFPHP. Just becuase you can keep state inside the Flash Player doesn't mean that you should. When the Flash Player first connects to AMFPHP it connects to gateway.php. The gateway runs a session_start which tries to reconnect to an existing php session. The NetConnection header will continue to send the session id to the server on all subsequent calls. You will need to use a debugging proxie in order to see what PHPSESSID is being sent with each call.

Counter sessions Tutorial

The following information is from the Counter example located with the AMFPHP-Examples download. The example shows you how each of these methods are used inside of flash/flex.

Counter Flash

When writing a service that requires a session it can be usefull to declare and retreive the session variable inside the constructor.

public function __construct() {
// Check if the session is available or create it.
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}

You now have access to the variable from inside your methods.

// Used to increment the session variable count.
public function increment() {
$_SESSION['count']++;
return $_SESSION['count'];
}

To destroy the session variable you can use unset.

// used to destroy the session variable and start over.
public function unregister() {
unset($_SESSION['count']);
return true;
}

To destroy the entire session use session_destroy.

// remove the entire session from the server.
public function destroy() {
session_destroy();
return true;
}

Change Session ID

It is sometimes necessary to sync sessions ID's on the server. This is becoming more prevelent with applications that have several flash files linked through local connection that want to share the same server state.

To get the current session ID use the following method

public function getSessionID() {
return session_id();
}

To set the session ID use the following method

// $id is the new session name
public function setSessionID($id) {
session_write_close();
session_id($id);
session_start();
return session_id();
}

 

Session error

If you get the following PHP error:

Notice: main() [function.main]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition “” of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in on line

You can get into this condition when a session is started however the session data is unserialized before the class is available. You can use the following code in your service’s constructor to work around that. This is common with Class Mapping VO's.

public function __construct() {
session_write_close();
session_start();

 


© amfphp.org | Disclaimer | Conditions of use