PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/cgi/example/cgi/sessions/main.cpp

http://github.com/darrengarvey/cgi
C++ | 63 lines | 29 code | 11 blank | 23 comment | 2 complexity | 9fd6f48da495664d23955b4b6aa1316b MD5 | raw file
  1. // -- sessions/main.hpp --
  2. //
  3. // Copyright (c) Darren Garvey 2009.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. ////////////////////////////////////////////////////////////////
  9. //
  10. //[cgi_sessions
  11. //
  12. // A simple example, using the default session type of a
  13. // std::map<string,string>. Session support is optional and
  14. // you should define BOOST_CGI_ENABLE_SESSIONS to use it.
  15. //
  16. #include <boost/cgi/cgi.hpp>
  17. #include <iostream>
  18. using namespace std;
  19. namespace cgi = boost::cgi;
  20. int main(int, char**)
  21. {
  22. try
  23. {
  24. cgi::request req;
  25. cgi::response resp;
  26. // You can use pick() to return a default value when the item
  27. // is not found in the request data.
  28. if (req.get.pick("clear", "") == "1") {
  29. req.stop_session();
  30. resp<< "Cleared session";
  31. return cgi::commit(req, resp);
  32. }
  33. // Start the session. It's safe to call `start_session()` if a session
  34. // is already open.
  35. req.start_session();
  36. // Output the current session data.
  37. resp<< cgi::content_type("text/plain")
  38. << "one = " << req.session["one"]
  39. << ", two = " << req.session["two"]
  40. << ", ten = " << req.session["ten"];
  41. // Modify the request session.
  42. req.session["one"] = "1";
  43. req.session["two"] = "2";
  44. req.session["ten"] = "10";
  45. // The session is saved by `commit()`.
  46. return cgi::commit(req, resp);
  47. } catch (std::exception& e) {
  48. cerr<< "Error: " << e.what() << endl;
  49. }
  50. cout<< "Content-type: text/html\r\n\r\nAn error occurred.";
  51. }
  52. //]