Is there a way to not save session ids & the session cookie in web2py on a per-controller level? global_settings.web2py_disable_session = True
will do it for the whole site, but I want some pages to retain sessions.
If you don't need to use the parameter-based rewrite system, you can disable sessions based on routing using the pattern-based rewrite system. In the routes.py
file, you would do something like this:
routes_in = [
('/myapp/default/$anything', '/myapp/default/$anything',
dict(web2py_disable_session=True))
]
The optional third element of a routes_in
tuple is a dictionary, which will be used to update request.env
. The above will add web2py_disable_session=True
to request.env
only for routes starting with /myapp/default/
(setting global_settings.web2py_disable_session=True
, on the other hand, will add web2py_disable_session=True
to request.env
for all requests).
Alternatively, you can simply call session.forget(response)
in any controller or action that does not need the session (or conditionally in a model file depending on the requested path). If no session cookie or file have yet been created, this will prevent their creation. Although simpler, this method is slightly less efficient than the above, as it will still result in the session initialization code running on every request.
One final alternative would be to create a custom WSGI application function in the WSGI handler file that conditionally adds web2py_disable_session=True
to the WSGI environment dictionary depending on the requested route. Then pass the modified environment dictionary to gluon.main.wsgibase
.