Saving data to on-rev

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
petero
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 59
Joined: Sat Jan 20, 2007 4:09 am

Saving data to on-rev

Post by petero » Thu Sep 10, 2009 12:15 pm

I am a newbie to the revlet world, an on-rev founder, and fresh off the conference simulcast. I have successfully written a revlet and have it running. I am not having any success saving the data that is in a field on a card in that stack so that it will appear the next time the stack opens on the web. Can someone show me a simple script that will save the contents of a field to on-rev and then read it back into the field next time the stack is opened? Any suggestions, places to look, or other resources are much appreciated. Thanks in advance.

Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Post by Philhold » Thu Sep 10, 2009 2:21 pm

So are you looking to have one users data available only to them or to everyone?

If it is only for them why do you need to store it on the server?

In other scripting languages when you want to have persistent data you write a cookie on the browser then when the user comes back you have a script check for the cookie and read it back into the script. I don't have a clue if this is an option for you but it is a suggestion.

Cheers

Phil

petero
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 59
Joined: Sat Jan 20, 2007 4:09 am

more details

Post by petero » Thu Sep 10, 2009 2:34 pm

Essentially, I want 4 users to be able to add items to the list of items that are in the field that is in the stack that is on on-rev. Each could then have access to looking at the entire list.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10057
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Post by FourthWorld » Thu Sep 10, 2009 3:29 pm

Anything served to a web browser is effectively unmodifiable. With Rev stack served up to the browser plugin, what the user gets is a copy of the stack sent from the server, just like the HTML page it sits in. Any modifications to the stack on the client's machine are limited to that client's machine, and will be lost at the end of the session when the stack goes away.

To allow modification of data in the stack you'll want to separate the data from the stack, and have a CGI on your server to which you can send any revised data for later retrieval.

Depending on the size and complexity of the data, how it's stored is up to you. You could use a database, but for small stuff that might be overkill. Since it's only four users you might be able to use simple text files easily enough.

In your stack, you could have a line which retrieves the data when the stack is opened. After the user has modified the data and wants to save it, the data is sent to a CGI, which writes it back to the file it came from for the next user.

To prevent overwriting data from other simultaneous users, your CGI could use a flag file when a save is happening, so that when another instance tries to save it looks for that file and waits to try again in a half second or so until it's done. Once a save it successfully complete the flag file is deleted.

Another option could be to have four separate data files, one for each user, with the stack obtaining the data from all four sources when it loads.

You can use the free Rev CGI engine for the CGI part of this, so you get to work on one language across the whole system.

There are links in this forum on setting up a CGI with the Rev engine, but please do post a note if you run into any snags.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Thu Sep 10, 2009 6:16 pm

Since you already have the on-Rev package, you could avoid the need for a "traditional" CGI setup.

By and large though, it is as Richard says.

http://www.screencast.com/users/Runtime ... 9ff5dcbd1a

is a video about the storing of guestbook data on the on-rev platform. You can adapt this as necessary to store the type of data you need and retrieve it appropriately.

In the revlet you can collect the data you need to store and put it into the right format name=value pairs and then

Code: Select all

post theData to url ("http://youronrevhost.on-rev.com/myDataStore.irev")
In the irev file you can read the values submitted in the $_POST array and store them in the text file you want, in a similar way to the guestbook entry.

Hope that helps

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Post by bangkok » Thu Sep 10, 2009 8:49 pm

SparkOut wrote: In the irev file you can read the values submitted in the $_POST array and store them in the text file you want, in a similar way to the guestbook entry.
Or he could store the data in a.... MySQL database (installed on the onrev server, via the cPanel).

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Thu Sep 10, 2009 9:42 pm

Indeed, but as Richard already discussed:
FourthWorld wrote:Depending on the size and complexity of the data, how it's stored is up to you. You could use a database, but for small stuff that might be overkill. Since it's only four users you might be able to use simple text files easily enough.
I was mentioning on-Rev options in the same vein. As you say, though, it's worth offering alternative ways, as we all know Rev provides many ways to skin that cat.
Hope all of these offerings are useful anyway, Petero.

petero
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 59
Joined: Sat Jan 20, 2007 4:09 am

Save data on On-Rev

Post by petero » Sun Sep 13, 2009 9:23 pm

Thanks so much for your replies. They are appreciated. I watched the video and tried out some of your suggestions. I guess I am not smart enough to see all the details.

Here is what, lacking the necessary knowledge, I tried to do:

I have a button in a stack (revlet) that is to store the contents of field "Goal" on on-rev for later retrieval.

on mouseUp
put field "Goal" of card 2 into theData
post theData to URL ("http://myaccount.on-rev.com/myDataStore.irev")
end mouseUp

I have a second button that will copy that stored data back to the "Goal" field.

on mouseUp
put URL ("http://myaccount.on-rev.com/myDataStore.irev") into theData
put theData into field "Goal" of card 2
end mouseUp

If you still think me worthwhile to respond to, it would be great to see how the above scripts might be written correctly.

In any case, thanks for your earlier responses. They have increased my Revolution knowledge even more.

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Sep 14, 2009 12:20 am

You need things on both the server side (on-Rev) and client side (the revlet) to make this work.

In your on-Rev hosting package you need to have an irev file that will handle the server side processing needed, in the case of the example below it would be called myGoalStore.irev and look something like:

Code: Select all

<?rev

if $_POST["goal"] is not empty then
  if $_POST["mysecret"] is base64encode(md5Digest ("OnlyIAsTheAdministratorKnowWhatThisCanPossiblyContain")) then
    put $_POST["goal"] into url "file:goalstore.txt"
  end if
end if

put url "file:goalstore.txt"

?>
This will put the submitted data into a text file called goalstore.txt in the same directory as your irev file. You can practice like this but you may want to make a folder that is outside the public_html directory on the on-Rev server and use something like url "file:/home/<yourOnRevHostId>/myNewDirectory/goalstore.txt" which will make it harder for people to change without authorisation (you will need to make sure this directory exists before the script tries to write the file in that location though). The data is also not processed or validated on the server, but it won't be submitted unless the preshared secret is known (sort of like a password) so it should filter out most bad submission attempts, and because you will probably have carriage returns in your field and maybe other chars which will break the post, we will base64 encode that before submission, as you will see below. The data (in its base64 encoded format, which is NOT secure) will be returned to any requesting browser/revlet/user, so you may want to do some extra encryption, or otherwise authenticate the connection to validate that it is a legitimate revlet user trying to retrieve the contents.

In your revlet you need to be able to send the post to the irev pageSend the data

Code: Select all

--  your field may have carriage returns in it and other characters
-- that will mess up the storage, so encode it as a single base64
-- string beforehand   
put base64encode(field "goal") into theData

-- specify a name for the "name=value" pair that will be submitted
put "goal" into theName

-- make a preshared secret "password"
put base64encode(md5Digest ("OnlyIAsTheAdministratorKnowWhatThisCanPossiblyContain")) into theSecretValue

-- you need a name to pair with the secret value too
put "mysecret" into theSecretName

-- this makes it a bit simpler to manage the creation the
-- name=value pairs if you adapt things for other projects
put libUrlFormData (theName,theData,theSecretName,theSecretValue) into theSubmission

-- send the encoded data to your irev page to be handled/stored
post theSubmission to url ("http://<yourOnRevHost/myGoalStore.irev")
and retrieve the data

Code: Select all

-- to retrieve the data that was stored on the on-Rev server
put url ("http://<yourOnRevHost/myGoalStore.irev") into theNewList
put base64decode(theNewList) into field "goal"
Hopefully that will get you started in a basic way. There are a couple of rudimentary safety checks in the above, but you might also consider the security implications of data handling and retrieval from the server.
HTH,
SparkOut

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Sep 14, 2009 1:13 am

Actually thinking again, a few changes can make things a little more secure - although some tightening should still be done. If you change the irev page so that it only (out)puts the goal text if the correct secret has been posted:

Code: Select all

<?rev 

if $_POST["mysecret"] is base64encode(md5Digest ("OnlyIAsTheAdministratorKnowWhatThisCanPossiblyContain")) then 
  if $_POST["goal"] is not empty then 
    put $_POST["goal"] into url "file:goalstore.txt" 
  else
    put url "file:goalstore.txt" 
  end if 
end if 

?>
then you will need to adjust the revlet when it retrieves the text file by submitting the
correct secret first, in order to get the server to output the goal text.

Code: Select all

-- to retrieve the data that was stored on the on-Rev server 
-- encode the preshared secret "password" 
put base64encode(md5Digest ("OnlyIAsTheAdministratorKnowWhatThisCanPossiblyContain")) into theSecretValue 

-- you need a name to pair with the secret value too 
put "mysecret" into theSecretName 

-- this makes it a bit simpler to manage the creation the 
-- name=value pairs if you adapt things for other projects 
put libUrlFormData (theSecretName,theSecretValue) into theSubmission 
post theSubmission to url ("http://<yourOnRevHost/myGoalStore.irev") 
put it into theNewList 
put base64decode(theNewList) into field "goal"

petero
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 59
Joined: Sat Jan 20, 2007 4:09 am

Saving to on-Rev

Post by petero » Mon Sep 14, 2009 2:21 am

Thank you, Sparkout, for your efforts. What a wonderful community exists on this forum. I wish that I could do something for you as well.

I am amazed at how much I don't know. It is humbling.

Thanks so much for giving me some of your precious time. I will try out your suggestions and hopefully be successful.

petero

Post Reply