1

Why can't my app find the session handler? I get this for an error:

Fatal error: Class 'Session' not found in /Users/Eamon/Sites/index.php on line 2

EDIT 2 (refactored index.php)

Here is my index.php:

<?php
class Session
{
private $savePath;

function open($savePath, $sessionName)
{
    $this->savePath = $savePath;
    if (!is_dir($this->savePath)) {
        mkdir($this->savePath, 0777);
    }

    return true;
}

function close()
{
    return true;
}

function read($id)
{
    return (string)@file_get_contents("$this->savePath/sess_$id");
}

function write($id, $data)
{
    return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
}

function destroy($id)
{
    $file = "$this->savePath/sess_$id";
    if (file_exists($file)) {
        unlink($file);
    }

    return true;
}

function gc($maxlifetime)
{
    foreach (glob("$this->savePath/sess_*") as $file) {
        if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
            unlink($file);
        }
    }

    return true;
}
}

$handler = new Session();
session_set_save_handler(
    array($handler, 'open'),
    array($handler, 'close'),
    array($handler, 'read'),
    array($handler, 'write'),
    array($handler, 'destroy'),
    array($handler, 'gc')
);

// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');

session_start();
// proceed to set and retrieve values by key from $_SESSION
// set time-out period (in seconds)
$inactive = 600;

// check to see if $_SESSION["timeout"] is set
if (isset($_SESSION["timeout"])) {
// calculate the session's "time to live"
$sessionTTL = time() - $_SESSION["timeout"];
if ($sessionTTL > $inactive) {
    session_destroy();
    echo "session destroyed;"
}
}
?>
<html>...<html>

<?php
session_destroy();
?>

session.php (I basically copied this from here: http://phpmaster.com/writing-custom-session-handlers/):

EDIT (added the interface as suggested by a below comment).

<?php
    interface SessionHandlerInterface
    {
        public function open($path, $name);
        public function read($sessionId);
        public function write($sessionId, $data);
        public function close();
        public function destroy($sessionId);
        public function gc($lifetime);
    }
class Session implements SessionHandlerInterface {
    // implement interfaces here

    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);    
    }


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

        $sql = "SELECT session_data FROM session where session_id =" . $db->quote($sessionId);
        $result = $db->query($sql);
        $data = $result->fetchColumn();
        $result->closeCursor();

        return $data;
    }


    function write($sessionId, $data) { 
        $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)
    }


    function close() {
        $sessionId = session_id();
        //perform some action here
    }


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

        $sql = "DELETE FROM session WHERE session_id =" . $db->quote($sessionId); 
        $db->query($sql);

        setcookie(session_name(), "", time() - 3600);
    }


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

        $sql = "DELETE FROM session WHERE session_lastaccesstime < DATE_SUB(NOW(), INTERVAL " . $lifetime . " SECOND)";
        $db->query($sql);
    }
}
?>

Thanks for any help!

UPDATE

I fixed a few errors...I edited my above code to reflect my changes, however I have a few new errors that need sorting out:

Warning: Wrong parameter count for session_set_save_handler() in /Users/Eamon/Sites/index.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Users/Eamon/Sites/index.php:1) in /Users/Eamon/Sites/index.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/Eamon/Sites/index.php:1) in /Users/Eamon/Sites/index.php on line 5

UPDATE 2

Apparently,

The function session_set_save_handler requires six parameters be passed to it.

I read this here: http://forums.phpfreaks.com/topic/18940-session-set-save-handler-problem/

UPDATE 3

Fixed the above parameter error...just put the session class directly in my index.php (I changed my code above to reflect my changes in index.php). Basically...I have exactly what you see in Example 2 here - http://php.net/manual/en/function.session-set-save-handler.php.

Here is the new error I am getting:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/Eamon/Sites/templates/showuser.php:1) in /Users/Eamon/Sites/templates/showuser.php on line 2

Here is showuser.php:

<?php
    session_start();

    $host="localhost"; // Host name
    $uname="root"; // Mysql username
    $password="bonjour3"; // Mysql password
    $db_name="itit"; // Database name
    $tbl_name="users"; // Table name

    // Connect to server and select database.
    $mysqli = mysqli_connect($host, $uname, $password, $db_name);
    $stmt = $mysqli->prepare("SELECT email FROM users WHERE username = ?");
    $stmt->bind_param("s", $_SESSION["username"]);
    $stmt->execute();
    $stmt->bind_result($em);
    $stmt->fetch();
?>

<h2>Username - Email</h2>
<div id="userinfo"><? echo $_SESSION["username"] ?> - <? echo $em ?></div>

<? 
    $stmt->close();
    mysqli_close($mysqli);
?>

Again...check the changes I made to index.php (the session.php file no longer exists).

ewizard
  • 2,801
  • 4
  • 52
  • 110

2 Answers2

2

You have to include the session.php file in order to access its class

<?php
include "path_to_session.php";
$handler = new Session();
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • @YogeshSuthar..fixed it - but now i get `Fatal error: Interface 'SessionHandlerInterface' not found in xxx` I found this - http://php.net/manual/en/class.sessionhandlerinterface.php. There is a comment below saying to add it to namespace...i dont know what that means though. – ewizard Jun 11 '13 at 14:43
  • @ewizard You have to also include path to that interface. – Yogesh Suthar Jun 11 '13 at 14:52
  • so i need to create the interface in a file called "SessionHandlerInterface.php"? Or can I dl it from somewhere...I don't have this file in my directory. – ewizard Jun 11 '13 at 14:59
  • 1
    figured the interface problem out...now getting a few errors...ill update my question to reflect my progress. – ewizard Jun 11 '13 at 15:07
  • @ewizard You are passing wrong parameter. See example 2 from here http://php.net/manual/en/function.session-set-save-handler.php – Yogesh Suthar Jun 11 '13 at 15:20
  • 1
    haha...i just changed all my code to reflect what is in that example...great minds think alike! i got rid of the error...one more error though...`Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/Eamon/Sites/templates/showuser.php:1) in /Users/Eamon/Sites/templates/showuser.php on line 2` I'll update my answer with what is on showuser.php (the profile page(. – ewizard Jun 11 '13 at 15:22
  • @ewizard See this question for this error http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – Yogesh Suthar Jun 11 '13 at 15:25
  • thanks for your continued support, i changed the "printf" statement to an "echo" statement in index.php (where inactivity gets checked (as the article said to do). Now i get `Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in /Users/Eamon/Sites/index.php on line 116` i updated my index.php to reflect the change you said to make in the above post. – ewizard Jun 11 '13 at 15:36
  • i realized that I wasnt showing where i had the destroy function...it is at the bottom of index.php...i just added it – ewizard Jun 11 '13 at 15:43
  • got it working...apparently the indentation in my storeuser.php was too far over - i indent within the code before i paste to stackoverflow...so this was causing the problem i think. – ewizard Jun 11 '13 at 17:10
1

Try to include it.

include 'session.php';
Sharon Haim Pour
  • 6,595
  • 12
  • 42
  • 64
  • @SharonHaimPourthanks...got it from above answer - but i am getting a new error if you read my comment above. – ewizard Jun 11 '13 at 14:44