How do I write a text file in ActionScript 3.0?

I am working on an authentication method and I have 2 text fields named 'username' and 'pass' I want to make it so that when the user enters their username and password, this information is saved in a text file. So when they log in, it reads the username and password from this text file to log in. How can i do this? Thanks: D

+3


source to share


3 answers


Saving a text file is possible (using the File class in AIR), but it's a really great approach. You should check out the SharedObject class instead

Quick example:

 var sharedObject:SharedObject = SharedObject.getLocal("userInfo"); //this will look for a shared object with the id userInfo and create a new one if it doesn't exist

      

Once you have a handle to your sharedObject

sharedObject.data.userName = "Some username";
sharedObject.data.password= "Some password"; //it really not a good idea to save a password like this
sharedObject.flush(); //saves everything out

      

Now, to get the data back, elsewhere in the code

var sharedObject:SharedObject = SharedObject.getLocal("userInfo");
trace(sharedObject.data.userName);
trace(sharedObject.data.password);

      



This object is saved locally on the user's computer. This is very similar to browser cookies.

Now, storing the password for this object as plain text is not a good idea. The best plan would be to check the login information on the server and store a session id of some kind in this object.

in pseudocode:

function validateLogin(){
    var sessionID = server->checkLogin(username, password); //returns a string if authed, nothing if not
    if(sessionID){
         sharedObject->sessionID = sessionID;
    } else {
        //bad login
    }
}

      

Additional Information:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html

http://www.republicofcode.com/tutorials/flash/as3sharedobject/

+5


source


using SharedObject

, see links:

http://www.flash-db.com/Tutorials/savingAS3/savingData.php?page=3



http://drawlogic.com/2008/01/10/howto-sharedobjects-for-local-storage-as3/

0


source


Ideally, you cannot write a file using flash (unless it is an AIR application). Flash must run in the browser, so there is a security risk to access system resources (such as your file system). However, AIR is a technology in which Flash works like a desktop application, and in that state it can write a file.

Shared object is an approach where flash can store some temporary data in the browser environment, but if you clear the cache and delete the browser files, I think this turns red too.

0


source







All Articles