Saving variabes and the echoing them.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Saving variabes and the echoing them.

Post 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!!)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Saving variabes and the echoing them.

Post 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.
rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Post by rpgfan »

Still confused :(
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Post 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...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How does Flash do it?

And why is this so hard?

Code: Select all

<?php
session_start();
$_SESSION['user'] = 'Bob';
?>
rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Post 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.
Last edited by rpgfan on Wed Feb 07, 2007 6:13 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Post 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...
Last edited by rpgfan on Wed Feb 07, 2007 6:44 pm, edited 2 times in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Firstly, use "

Code: Select all

" when showing php code.

Secondly, every statement of code ends with a semicolon.
rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Post by rpgfan »

I realized that. In flash, you can leave out the semicolon. Thats a habit im going to have to break.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
rpgfan
Forum Newbie
Posts: 11
Joined: Wed Feb 07, 2007 5:37 pm

Post by rpgfan »

Im having trouble reading the file in sing unserialize()
Last edited by rpgfan on Wed Feb 07, 2007 7:33 pm, edited 1 time in total.
Post Reply