0

Below is copied directly from the php manual, I'm not sure how to set this up, there are just two things I need to do..

  1. write some code when the session is destroyed
  2. set the session_cache_expire to 20 mins - do that in the class or as a separate call?

Do I need to run sessionsavehandler() for every instance, every time I have session_start on the page? If someone could please outline the steps I need to take to use this class that would be great.

<?php
 new SessionSaveHandler();
 ?>

 <?php

class SessionSaveHandler {
     protected $savePath;
     protected $sessionName;

    public function __construct() {
         session_set_save_handler(
             array($this, "open"),
             array($this, "close"),
             array($this, "read"),
             array($this, "write"),
             array($this, "destroy"),
             array($this, "gc")
         );
     }

    public function open($savePath, $sessionName) {
         $this->savePath = $savePath;
         $this->sessionName = $sessionName;
         return true;
     }

    public function close() {
         // your code if any
         return true;
     }

    public function read($id) {
         // your code
     }

    public function write($id, $data) {
         // your code
     }

    public function destroy($id) {
         // your code
     }

    public function gc($maxlifetime) {
         // your code
     }
 }

new SessionSaveHandler();

?> 

1 Answer 1

1

I'm not sure how to set this up, do I need to run sessionsavehandler for every instance, every time I have session_start on the page?

Yes, you would just create a new instance of SessionSaveHandler instead of before calling session_start.

I want to change the sesison_cache expire as well or should I do that in the class?

You are just overriding the default storage handler; the rest stays unchanged. So use session_cache_expire as you would before.

If someone could please outline the steps I need to take to use this class that would be great.

As already said, you are just overriding PHP’s default session storage handler. So instead of before calling session_start create a new instance of your session storage handler SessionSaveHandler. Apart from that, nothing else is different from using PHP’s default session handler.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi I set this up to call the class with this in the open function of the class $this->datetime = date("d/m/Y H:i:s"); mail("[email protected]","session opened","start: ".$this->datetime);' i'm not getting any emails though
@jimsmith Sorry, I was wrong. You need to call session_start anyway.
thanks I set up the session_cache_expire and session_start to run in the construct and that is running OK, but I'm still not getting anything from the open function? Should the open function run automatically when the session started? I'm not getting anything from the destroy() either
I was hoping that the destroy method would trigger when the cache expired that is why I'm using it.
@jimsmith session_cache_expire has nothing to do with session expiration. It just controls how long an HTTP response of an active session is considered fresh according to HTTP caching.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.