Page 1 of 2

Saving variabes and the echoing them.

Posted: Wed Feb 07, 2007 5:53 pm
by rpgfan
Hello. Ive been a journeyman (lol oblivion) in flash for quite a while now, and ive decided to learn a bit of php for simple communication. Nothing big, just some array passings. But, ive hit a problem. The variables are doestroyed when the php file ends (duh, why didnt I think of that). I looked up some saving tuts on php up on google, but none are simple enough for a php n00b like me, as a matter a fact, the only reason I can even function in php is beacase it like actionscript (flash coding). I need a saving template that can work wih echoing. (So the php can communicate w/ flash!!)

Re: Saving variabes and the echoing them.

Posted: Wed Feb 07, 2007 5:56 pm
by RobertGonzalez
rpgfan wrote:Hello. Ive been a journeyman (lol oblivion) in flash for quite a while now, and ive decided to learn a bit of php for simple communication. Nothing big, just some array passings. But, ive hit a problem. The variables are doestroyed when the php file ends (duh, why didnt I think of that). I looked up some saving tuts on php up on google, but none are simple enough for a php n00b like me, as a matter a fact, the only reason I can even function in php is beacase it like actionscript (flash coding). I need a saving template that can work wih echoing. (So the php can communicate w/ flash!!)
Look into PHP sessions.

Posted: Wed Feb 07, 2007 5:59 pm
by rpgfan
Still confused :(

Posted: Wed Feb 07, 2007 6:03 pm
by RobertGonzalez
Basically PHP does not maintain user state. So what happens on one page will not carry to any subsequent pages. The rememdy for this (such as in the case if login scripts, user management, shopping carts) is to put in place a mechanism by which your app can maintain user state. That is accomplished, on one of many ways, by PHP Sessions.

I would google 'PHP Sessions'. I am sure there are tons of tutorials on the subject.

Posted: Wed Feb 07, 2007 6:05 pm
by rpgfan
Ugh, saving variables in flash is alot eaier thatn this... too bad php and FlashCom is the only way for falsh to communicate with a web server...

Posted: Wed Feb 07, 2007 6:06 pm
by RobertGonzalez
How does Flash do it?

And why is this so hard?

Code: Select all

<?php
session_start();
$_SESSION['user'] = 'Bob';
?>

Posted: Wed Feb 07, 2007 6:09 pm
by rpgfan
Thanks. I tend to learn form example better than tutorials. How do I control where the variables are saved?

Flash saves like this:

Code: Select all

myLSO = SharedObject.getLocal("[name]"); 


myObj.objArray = new Array(); 

myObj.objArray[0] = "apple"; 
myObj.objArray[1] = "orange"; 
myObj.objArray[2] = "pear"; 


myLSO.data.myObj = myObj; 

}
Pretty strieght forward.

Posted: Wed Feb 07, 2007 6:12 pm
by RobertGonzalez
The data is saved on the server by PHP in a location specified by php.ini. You access the data by calling session_start() at the top of the script, then by accessing the related members of the $_SESSION superglobal array.

Essentially you set a $_SESSION array variable, then it becomes available on all pages that are under session management. As long as the time of the sessions is with the session garbage collection time limit (which by default is 24 minutes) the data should be readily available to you.

Posted: Wed Feb 07, 2007 6:16 pm
by rpgfan
To tell you the truth, I would prefer serializing the variables into a .txt file using fputs and all of that stuff... i ust dont know how to use them correctly. Also, I need the variables to last alot longer than 24 minutes.

EDIT: Hold up, I think i found a tut. Il get back to you if it works.

Posted: Wed Feb 07, 2007 6:28 pm
by superdezign
Well maybe you should try thinking in terms of Flash ActionScript. Dynamic text in Flash can be given an id and a value, as well as regular variables. So, save the variables from your PHP code into a dynamic text box, then into variables, and pass those variables from one php script to the next.

In Flash, think Flash. It's a powerful tool.

Posted: Wed Feb 07, 2007 6:39 pm
by rpgfan
A more accurate mtaphor is flash saving a .sol file on your harddrive for other swf's to read.

UPDATE: I can get serialize to write. But, in this code:

Code: Select all

<?php
$chatbox = "lolz"
$res = fopen("chatbox.txt","r+"); // Opens a resource
$string = serialize($chatbox); // Sets a variable to identify the serialized array
$write = fwrite($res,$string); // Writes it to the file
if($write) {
[size=18]echo $chatbox[/size]
}
else {
echo "There was a problem.";
}
@fclose($res);
?>
When I open the file, it doesent say lolz, its just blank, wtf?

EDIT: Oh, i forgot the colon.... php is alot touchier than flash...

Posted: Wed Feb 07, 2007 6:42 pm
by superdezign
Firstly, use "

Code: Select all

" when showing php code.

Secondly, every statement of code ends with a semicolon.

Posted: Wed Feb 07, 2007 6:44 pm
by rpgfan
I realized that. In flash, you can leave out the semicolon. Thats a habit im going to have to break.

Posted: Wed Feb 07, 2007 6:47 pm
by RobertGonzalez
Read the example on the fwrite() page of the manual. It gives a brief, but usable, mini-tutorial on how to write to files.

Posted: Wed Feb 07, 2007 7:31 pm
by rpgfan
Im having trouble reading the file in sing unserialize()