First of all: PHP runs on the server and therefore can send the necessary HTTP header to set the cookie, Flash runs in the client’s browser and, therefore, cannot do the same.
However, it is possible to access and store cookies from flash / flex by using a function flash.external.ExternalInterfaceand calling JavaScript to receive and set cookies.
I developed a class for this very easily:
package de.slashslash.util {
import flash.external.ExternalInterface;
public class Cookie {
private static var _initialized:Boolean = false;
private var _name:String;
private var _value:String;
private var _isNew:Boolean;
private static const GET_COOKIE:String = "cookieGetCookie";
private static const SET_COOKIE:String = "cookieSetCookie";
private static var FUNCTION_GET_COOKIE:String =
"function () { " +
"if (document." + GET_COOKIE + " == null) {" +
GET_COOKIE + " = function (name) { " +
"if (document.cookie) {" +
"cookies = document.cookie.split('; ');" +
"for (i = 0; i < cookies.length; i++) {" +
"param = cookies[i].split('=', 2);" +
"if (decodeURIComponent(param[0]) == name) {" +
"value = decodeURIComponent(param[1]);" +
"return value;" +
"}" +
"}" +
"}" +
"return null;" +
"};" +
"}" +
"}";
private static var FUNCTION_SET_COOKIE:String =
"function () { " +
"if (document." + SET_COOKIE + " == null) {" +
SET_COOKIE + " = function (name, value) { " +
"document.cookie = name + '=' + value;" +
"};" +
"}" +
"}";
private static function initialize():void {
if (Cookie._initialized) {
return;
}
if (!ExternalInterface.available) {
throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");
}
ExternalInterface.call(FUNCTION_GET_COOKIE);
ExternalInterface.call(FUNCTION_SET_COOKIE);
Cookie._initialized = true;
}
public function Cookie(name:String) {
Cookie.initialize();
this._name = name;
this._value = ExternalInterface.call(GET_COOKIE, name) as String;
this._isNew = this._value == null;
}
public function get name():String {
return this._name;
}
public function get value():String {
return this._value;
}
public function set value(value:String):void {
this._value = value;
ExternalInterface.call(SET_COOKIE, this._name, this._value);
}
public function get isNew():Boolean {
return this._isNew;
}
}
}