Anniepoo/swiplwebtut

sessions assumes you understand http sessions already

Closed this issue · 1 comments

Had a number of users who have gaps in their knowledge of http. Here's a coherent example of sessions sent to one of them. Might be worth including.

user one comes to the site. If they visit a page in a file where we've loaded module library(http/http_session)
then they'll have a 'session' established by the time they reach the handler.

The example provided with the tutorial gives the user a silly name. Once the name's established, it will be used on
subsequent visits as long as the session lasts. Most systems keep sessions about 30 minutes after the last activity
(its settable in swi-prolog).
Whats going on 'under the covers' is that the first httpRequest includes in its' response a set-cookie header with a
random number. Subsequent requests pass this cookie back to the server, so the user gets a persistent identity.
Now data can be associated with that identity using http_session_asserta and http_session_data
Here's the sequence using the tutorial example:

Bob Joe
requests page
server assigns him session ID12345
First clause of nick_name fails because we don't have nick_name(Nick)
in the session database
example code randomly picks 'Wise Soul' as Bob's nick
using the second clause of nick_name. This gets stored
in the session database by http_session_assert.

reloads page
this time his browser includes cookie ID12345
first clause of nick_name succeeds, since we've already
got the nick_name in the session database
so Nick is bound to 'Wise Soul'

                                                                                                                                                                                                                                                                                                                                    requests page
                                                                                                                                                                                                                                                                                                                                    server assigns him session ID6666
                                                                                                                                                                                                                                                                                                                                    First clause of nick_name fails because we don't have nick_name(Nick)
                                                                                                                                                                                                                                                                                                                                    in Joe's session database, which is distinct from Bobs
                                                                                                                                                                                                                                                                                                                                    example code randomly picks 'Gentle One' as Bob's nick 
                                                                                                                                                                                                                                                                                                                                    using the second clause of nick_name. This gets stored
                                                                                                                                                                                                                                                                                                                                    in the session database by http_session_assert.

reloads page again
his browser again includes cookie ID12345
first clause of nick_name succeeds, since we've already
got the nick_name in the session database
so Nick is bound to 'Wise Soul' again
(and will be as long as Bob occasionally reloads the page)

                                                                                                                                                                                                                                                                                                                                    reloads page
                                                                                                                                                                                                                                                                                                                                    this time his browser includes cookie ID6666
                                                                                                                                                                                                                                                                                                                                    first clause of nick_name succeeds, since we've already
                                                                                                                                                                                                                                                                                                                                    got the nick_name in the session database
                                                                                                                                                                                                                                                                                                                                    so Nick is bound to 'Gentle One'

So, we've got two people using the site, and each one has his own data.

You need to do something similar, but your version of nick_name will save and get the game state.

added,