0

Hi I want to add some functionality to the session_start function in php. I know I can write a custom function to replace session_start() but that goes too far. I just want to have some additional code trigger, not remove the old code.

Edit: Specifically I want the session ID to be written into a sql table.

7
  • 5
    Add what functionality? Because there are probably things like SessionHandler that will accomplish what you want without having to modify the PHP source and compile your own binaries. Commented Mar 19, 2014 at 22:44
  • ^ this. What functionality? You could easily build your own session class too, which initiates session start in the construct and performs your other code too. Commented Mar 19, 2014 at 22:45
  • edited to add details Commented Mar 19, 2014 at 22:55
  • I think that Session Handler link may be what I'm looking for Commented Mar 19, 2014 at 22:58
  • @AdamSturge, it is. Or you can just write a session_start() of your own as Mike Brant suggests. It depends on where you want to put the code. "auto prepend" could also be of use. Commented Mar 19, 2014 at 23:02

1 Answer 1

1

You could simply create a function wrapper around session_start() that you use.

function my_session_start() {
    session_start();
    $session_id = session_id();
    // write $session_id to database by whatever method you choose
}

// usage
my_session_start();

Or if you want to extend on sessionHandler you could do something like this:

class mySessionHandler extends sessionHandler {
    // perhaps a property to store db connection or DB object as needed for writing session id to database
    protected $db = null;

    public function __construct($db = null) {
        if(is_null($db)) {
            throw new Exception('Give me a database');
        }
        // maybe some other validation (could also use type hinting in parameter
        $this->db = $db;
    }

    public function open($save_path, $session_id) {
        parent::open($save_path, $session_id);
        // not shown - use $this->db to insert $session_id to database
    }
}

// usage
$session_handler = new mySessionHandler($db);
session_set_save_handler($session_handler, true);
session_start();

Here you are only overriding the open() method to add the session id to the database.

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

5 Comments

yeah I thought of this way but I wanted to know if there was a way to do it without using a wrapper.
@AdamSturge I updated answer with another option that may meet your needs without requiring you to actually specify any customer session handling logic. I would say in general that this is a little odd. You might consider simply biting the bullet and moving all session storage to DB via a custom class implementing SessionHandlerInterface (you would not want to extend SessionHandler for this sort of thing).
Thanks. I may move all session storage to the database in the future since having it split between two sources leaves a bad taste in my mouth. However this is what I need for the moment.
@AdamSturge Glad to help. It's just that there is probably little value in storing session id's in a database, other than being able to have a historical record of all sessions that have been created. It won't tell you current sessions unless you also delete items from the list on session destroy.
Well to be honest I plan on storing more than just session id in the table, that's just the only bit that was relevant to the issue at hand. I am planning to delete them on session_destroy though. I assumed that once I knew how to do it for session_start that session_destroy would be similar.

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.