PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/cgi/example/cgi/custom_sessions/main.cpp

http://github.com/darrengarvey/cgi
C++ | 47 lines | 29 code | 12 blank | 6 comment | 2 complexity | 0c1c187ae23ff4254ab3cb5f4c65e6e9 MD5 | raw file
  1. //[custom_sessions
  2. #include <boost/cgi/cgi.hpp>
  3. #include <iostream>
  4. using namespace std;
  5. using namespace boost::cgi;
  6. int main(int, char**)
  7. {
  8. try
  9. {
  10. request req;
  11. response resp;
  12. if (req.get.pick("clear", "") == "1") {
  13. req.stop_session();
  14. resp<< "Cleared session";
  15. return commit(req, resp);
  16. }
  17. // Start the session. It is safe to call `start_session()` if a session
  18. // is already open, but you shouldn't have to do that...
  19. req.start_session();
  20. resp<< content_type("text/plain")
  21. << "one = " << req.session["one"]
  22. << ", two = " << req.session["two"]
  23. << ", ten = " << req.session["ten"];
  24. // Modify the request session.
  25. req.session["one"] = "1";
  26. req.session["two"] = "2";
  27. req.session["ten"] = "10";
  28. // The session is saved by `commit()`.
  29. return commit(req, resp);
  30. } catch (std::exception& e) {
  31. cerr<< "Error: " << e.what() << endl;
  32. }
  33. cout<< "Content-type: text/html\r\n\r\nAn error occurred.";
  34. }
  35. //]