title: Session meta:

  • name: description content: EasySwoole Session processing component
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole Session processing component|swoole session|php session

Session

Easyswoole Since 3.2.x, the default session service is no longer available. If you need to use the user, please introduce the session component independently.

Installation

  1. composer require easyswoole/session

Use

Define a basic session controller, and inherit the AbstractSessionController parent class. The rest of the controllers only need to inherit the base session controller to implement the session call.

  1. use EasySwoole\Session\FileSessionHandler;
  2. use EasySwoole\Session\Test\RedisHandler;
  3. use EasySwoole\Session\AbstractSessionController;
  4. class RedisHandler extends AbstractSessionController
  5. {
  6. protected function sessionHandler(): \SessionHandlerInterface
  7. {
  8. /*
  9. * The link should be taken here by the connection pool, otherwise the actual production will lead to the continuous creation of the link.
  10. */
  11. $redis = new \Redis();
  12. $redis->connect('127.0.0.1');
  13. return new RedisHandler($redis);
  14. }
  15. function index()
  16. {
  17. $this->session()->start();
  18. $time = $this->session()->get('test');
  19. if($time){
  20. $this->response()->write('session time is '.$time);
  21. }else{
  22. $this->session()->set('test',time());
  23. $this->response()->write('session time is new set');
  24. }
  25. }
  26. }
  27. class FileHandler extends AbstractSessionController
  28. {
  29. protected function sessionHandler(): \SessionHandlerInterface
  30. {
  31. return new FileSessionHandler();
  32. }
  33. function index()
  34. {
  35. $this->session()->start();
  36. $time = $this->session()->get('test');
  37. if($time){
  38. $this->response()->write('session time is '.$time);
  39. }else{
  40. $this->session()->set('test',time());
  41. $this->response()->write('session time is new set');
  42. }
  43. }
  44. }

::: warning The built-in file session implementation is lock-free. :::

List of supported methods

  • gcMaxLifetime()
  • gcProbability()
  • savePath()
  • sessionId()
  • start()
  • sessionName()
  • set()
  • get()
  • unset()
  • destroy()
  • close()
  • gc()