Anybody succesfully using sessions?

Are you using LiveCode to create server scripts or CGIs?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Anybody succesfully using sessions?

Post by malte » Fri Jan 20, 2012 1:24 am

Hi there,

I am really stumped on this. I do not get start session to work as I would think it should. As soon as I reload the page or switch to another site (in the same folder) $_SESSION is empty.

Code: Select all

<?lc
put the globalnames
set the sessionSavePath to "/test"
set the sessionid to "100"


if $_SESSION["counter"] is empty then  
start session
put " After start:"&&the globalnames
  add 1 to $_SESSION["counter"]
  put "No previous session detected. Session counter=" & $_SESSION["counter"]
else
  add 1 to $_SESSION["counter"]
  put "Session counter:" & $_SESSION["counter"]
end if
?>
Can anyone post an example that works using $_SESSION?

Cheers,

Malte

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Anybody succesfully using sessions?

Post by sturgis » Fri Jan 20, 2012 5:11 am

I just got them to work, took a bit of experimenting and digging into the dictionary but I'll post a working example. I simplified slightly and don't use a sessionid (so that rev will just generate it all) Also, since I don't have a /test directory for now i'm just putting things into the default folder IE where the .lc file is.

Make sure you have write permission to whatever folder of course.

Code: Select all

<?lc
put the globalnames into tOldGlobal 
-- don't want to write anything till AFTER session starts or it can cause issues

set the sessionSavePath to the defaultfolder -- saving session stuff to the same dir as the .lc

start session 
-- start the session. If you don't do this BEFORE your if check, $_SESSION["counter"] will always be empty

if $_SESSION["counter"] is empty then  
put "Fred" into $_SESSION["username"]
 -- just added a key to see it work

put 1 into $_SESSION["counter"] 
-- using a counter as you were implementing

  put merge("Before start: [[tOldGlobal]] After start: [[the globalnames]]")& "<br>"
 -- shows the global vars before and after

else 
-- if there is a session do the else

put $_SESSION["username"] & "<br>" 
-- should put the username defined on the first iteration

put "Session detected: Session counter=" & $_SESSION["counter"] & "<br>" 
-- same here, puts the current counter

  add 1 to $_SESSION["counter"]
  put "Incrementing Session counter:" & $_SESSION["counter"] 
-- increments and displays the future counter

end if

stop session
 -- this is critical.  The changes aren't written out to file if you don't explicitly "stop session" 
-- so you end up with session files of 4k with no data.
-- after stop session, file is written, and you're good to go for the next round. 
?>

EDIT: If you want to see this example in action go to http://guidezone.info/test.lc

Edit again: Fixed the link.
Also 1 last thing, the way things are setup with on-rev (if thats what you're using) .irev files default to version 3.5 of the engine. .lc files use the most current engine.

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Re: Anybody succesfully using sessions?

Post by malte » Fri Jan 20, 2012 8:52 am

Yes!!!!

Thank you so much. Calling stop session does the trick!

You are officially my hero now. :-)

Thank you so much,

Malte

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Anybody succesfully using sessions?

Post by sturgis » Fri Jan 20, 2012 2:04 pm

I owe you thanks too. I hadn't the remotest clue how to even get started with this stuff until your script pointed me the right direction, so thanks for that! Glad its working!
malte wrote:Yes!!!!

Thank you so much. Calling stop session does the trick!

You are officially my hero now. :-)

Thank you so much,

Malte

Post Reply

Return to “CGIs and the Server”