【问题标题】:Fatal error: Class 'xxx' not found [closed]致命错误:找不到类“xxx”[关闭]
【发布时间】:2013-06-07 11:07:41
【问题描述】:

为什么我的应用找不到会话处理程序?我得到这个错误:

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

EDIT 2(重构 index.php)

这是我的 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(我基本上是从这里复制过来的:http://phpmaster.com/writing-custom-session-handlers/):

EDIT(按照以下评论的建议添加界面)。

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

感谢您的帮助!

更新

我修复了一些错误...我编辑了上面的代码以反映我的更改,但是我有一些新的错误需要整理:

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

更新 2

显然,

函数 session_set_save_handler 需要传递六个参数。

我在这里读到这个:http://forums.phpfreaks.com/topic/18940-session-set-save-handler-problem/

更新 3

修复了上面的参数错误...只需将会话类直接放在我的 index.php 中(我更改了上面的代码以反映我在 index.php 中的更改)。基本上......我有你在示例 2 中看到的内容 - http://php.net/manual/en/function.session-set-save-handler.php

这是我得到的新错误:

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

这里是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);
?>

再次...检查我对 index.php 所做的更改(session.php 文件不再存在)。

【问题讨论】:

    标签: php mysql class session fatal-error


    【解决方案1】:

    您必须包含 session.php 文件才能访问其类

    <?php
    include "path_to_session.php";
    $handler = new Session();
    

    【讨论】:

    • @YogeshSuthar.. 修复了它 - 但现在我得到了 Fatal error: Interface 'SessionHandlerInterface' not found in xxx 我发现了这个 - php.net/manual/en/class.sessionhandlerinterface.php。下面有一条评论说要将它添加到命名空间...不过我不知道这是什么意思。
    • @ewizard 您还必须包含该接口的路径。
    • 所以我需要在一个名为“SessionHandlerInterface.php”的文件中创建接口?或者我可以从某个地方下载它...我的目录中没有这个文件。
    • 解决了界面问题...现在出现了一些错误...我会更新我的问题以反映我的进度。
    • 哈哈...我只是更改了所有代码以反映该示例中的内容...伟大的思想都一样!我摆脱了错误......不过还有一个错误......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我将用showuser.php上的内容更新我的答案(个人资料页面(。
    【解决方案2】:

    尝试include它。

    include 'session.php';
    

    【讨论】:

    • @SharonHaimPourthanks...从上面的答案中得到 - 但如果你阅读我上面的评论,我会收到一个新错误。
    猜你喜欢
    • 2023-03-10
    • 2016-04-09
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2011-09-08
    • 2011-12-17
    • 2018-12-21
    • 2012-11-21
    相关资源
    最近更新 更多