0

I am trying to set up a user login/logout system using php and mysql. I have found some documentation on the subject here: http://phpmaster.com/writing-custom-session-handlers/. I am trying to follow along with it (I have also been pulling from other sources too - but this is the main one).

Here is some of my code from "my_session_handler.php":

class MySessionHandler implements SessionHandlerInterface {

    private $path = session_save_path();
    private $name = session_name();
    private $sessionId = session_id();

    function open($path, $name) {
        $db = new PDO("mysql:host=localhost;dbname=itit", "root", "bonjour3");

        $sql = "INSERT INTO session SET session_id =" . $db->quote($sessionId) . ", session_data = '' ON DUPLICATE KEY UPDATE session_lastaccesstime = NOW()";
        $db->query($sql);    


    ...

My question is, where are the "$path" and "$name" variables coming from in the example I cited above? I declared them as private variables and used some functions to do what I am thinking needs to be done. But on the website that I am following along with - neither of these variables get declared - along with $sessionId. I see that the read function returns $data. So I used it in the "write" function like this:

function write($sessionId, this.read($sessionId)) { 
    $db = new PDO("mysql:host=localhost;dbname=itit", "root", "bonjour3");

    $sql = "INSERT INTO session SET session_id =" . $db->quote($sessionId) . ", session_data =" . $db->quote($data) . " ON DUPLICATE KEY UPDATE session_data =" . $db->quote($data);
    $db->query($sql)
}

Am I doing this right?

1 Answer 1

1

These parameters are used by the default session save handler, which saves the session data in files. They come from the php.ini file, and are used to form the filenames. They're supplied to your handler when PHP calls the open() method.

If you're writing a custom handler, you can ignore them, as the code on that web page does.

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

2 Comments

barmar...i have one more question - do i need to go about actually using the functions in the MySessionHandler? For example...do I need to use the close() function when I want to close a session? Or does the session handler do all that for me?
You don't use these functions directly, they're used by the session handler. When the application calls session_close(), the session handler calls your close function.

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.