爲什麼我的應用程序找不到會話處理程序?我得到這個一個錯誤:致命錯誤:未找到'xxx'類
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/):
編輯(添加如下評論建議的界面)。
<?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);
}
}
?>
感謝您的幫助!
UPDATE
我定了一些錯誤......我修改了上面的代碼,以反映我的變化,但是我有需要整理出了一些新的錯誤:
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
顯然,
The function session_set_save_handler requires six parameters be passed to it.
我這個在這裏讀到:http://forums.phpfreaks.com/topic/18940-session-set-save-handler-problem/
更新3
修正上述參數錯誤...只是把session類直接在我的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文件不再存在)。
@ YogeshSuthar..fixed它 - 但現在我得到了'致命的錯誤︰Interface'SessionHandlerInterface'找不到xxx'我發現這個 - http://php.net/manual/en/class.sessionhandlerinterface.php。下面有一條評論說要將它添加到名稱空間...我不知道這是什麼意思。 – ewizard
@ewizard你還必須包含該接口的路徑。 –
所以我需要在一個名爲「SessionHandlerInterface.php」的文件中創建接口?或者我可以從某處取消它...我的目錄中沒有這個文件。 – ewizard