Before using a session, you need to understand what the session is and what the actual form of the session is in programming. Here, a web page is clear, posted directly [although this page at first glance know is not original, but original I was not found] http://www.2cto.com/kf/201206/135471.html
As we have already said on the above page, sessions are stored and controlled on the server. on the client side, only one session credential can be saved temporarily-the session ID; the client saves session IDs in a variety of ways: URL suffixes, hidden fields of webpages, and cookies. The most common method is Cookie, other cases are considered only when cookies are disabled on the client. The session ID stored by cookies can also be saved by hard disk or memory; the hard disk storage method will still take effect when it is enabled again after the browser is closed [depends on the cookie validity period], and the memory storage mode will lose session once the browser is closed
The sharing mechanism for storing sessions in different browsers in the memory mode is also different. FF means that all tabs of the same domain name share a session ID, the session ID is not shared for different tab pages in the same field in IE. Only the inherited tab page shares the session ID with the parent tab page. [The New Page opened by Ctrl + n is the inheritance page ], so even the memory session will have a session string. [Testers can try more, huh, huh]
At this point, there is basically nothing on the client, and all the main work on the session is done on the server side. First, the server will detect each user's request and obtain the relevant information of the user's request (for example: IP, Cookie, the server first checks whether there is a specific session ID field [of course the field set last time]. If it does not indicate that this client was the first time or has interrupted the request, therefore, a new session object is generated and a session ID is generated. During this user access, the server can record useful user information and save it under the session ID tag; after the server completes processing, set the session ID as a cookie in the Response Header. The session ID will be generated when the request comes again next time.
If the user ID can be queried, the client is not the first request. The server can also use the session ID to query the user information retained by previous user operations, this ensures the state and information continuity during multiple consecutive accesses. Http itself is stateless and cannot achieve this effect without the session mechanism.
As for the creation of session objects and the setting of all session ID response headers are standardized, webpy also provides a module that supports the session function. For how to use it, see the following:
Import Webweb. config. DEBUG = falseurls = ("/count", "Count", "/RESET", "reset") APP = web. application (URLs, locals () Session = web. session. session (app, Web. session. diskstore ('session'), initializer = {'Count': 0}) Class count: def get (Self): Session. count + = 1 return STR (Session. count) Class Reset: def get (Self): Session. kill () # end a session return "" If _ name _ = "_ main _": app. run ()
The client session ID can be saved to both the memory and the hard disk, while the memory of the server session can also be saved to the memory, hard disk, or even the database, all information that can be stored, while webpy only supports two formats: hard disk and database. If you need to use other storage methods, you can write a new web. utils subclass, and implement the corresponding method, and then replace
web.session.DiskStore('sessions')
Of course, the session storage on the server end can also be set to the validity period, because if there is no expiration period, even if the memory is larger, the hard disk will always be filled up by the session one day, therefore, when using the session, remember to set the expiration time. In addition to setting the expiration time for the session to expire on the server side, you can also proactively close the session through client requests.
session.kill()
Another thing to note is that the above Code is in a file, and the session is the global variable in the current file, so the session can be shared; and daily programming cannot have all the content in one file, session is not a global variable at the web level, so if you want to use the same session in the Project of multiple files, you need to set the session to a globally accessible location during server initialization, such as web. config,
web.config._session = session
In this way, you can access the session content through web. config. _ session only when you reference the web module on the page. However, the session cannot be used in the debug mode by default, so the official session will disable the debug mode. DEBUG = false, because the debug mode will reload the module, and the session will be reloaded repeatedly. In fact, you can use the following code to avoid disabling the debugging mode:
if web.config.get("_session") is None: from web import utils store = web.session.DiskStore('sessions') user = utils.Storage({ "id": "", "name": "", "email": "", "privilege": "", }) session = web.session.Session(app, store, initializer={ "status": 0, "user": user, }) web.config._session = sessionelse: session = web.config._session
There is also a way to make the session globally available. Add the following code on the main program page:
def session_hook(): web.ctx.session = sessionapp.add_processor(web.loadhook(session_hook))
Call Session on other pages:
print web.ctx.session.testweb.ctx.session.foo = 'bar'
In addition, webpy provides an entry for setting the session. You can use these settings to set attributes of the session, such as the validity period:
web.config.session_parameters['cookie_name'] = 'webpy_session_id'web.config.session_parameters['cookie_domain'] = Noneweb.config.session_parameters['timeout'] = 86400, #24 * 60 * 60, # 24 hours in secondsweb.config.session_parameters['ignore_expiry'] = Trueweb.config.session_parameters['ignore_change_ip'] = Trueweb.config.session_parameters['secret_key'] = 'fLjUfxqXtfNoIldA0A0J'web.config.session_parameters['expired_message'] = 'Session expired'
References:
Http://www.2cto.com/kf/201206/135471.html
Http://webpy.org/cookbook/userauthpgsql
Http://webpy.org/cookbook/sessions.zh-cn