note 80413 added to faq
| From: | vlad-45 at voliacable dot com | Date: | Mon, 14 Jan 2008 22:42:59 +0000 |
| Subject: | note 80413 added to faq | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135102@lists.php.net to get a copy of this message | ||
I spend too much time with session_set_save_handler() but could not use it.
I work with Apache/1.3.12 (Win32) PHP/5.1.2 in local mode, if it matters. I want to use handler
destroy() but it does not
work. If I need to determine all handlers, then I have a problem with write().
Function write() does not want to create and open storage file. If file created outside the
function, it still would not be opened. Actually, write() refuses to create any file. But debug
output shows
that write() was called and had all the necessary data.
This problem can be solved by removing of function write(). Session will create storage file and
write
data in it. But function destroy() refuses to work in any case. (It's the only function I
need.)
After closing of browser the storage file remains and file
temp (debug output) is absent.
Maybe closing of browser does not mean end of session (although next time the session id will be
different),
but let's see how function write() works:
<?php
function open($save_path, $session_name)
{
global $sess_save_path;
$sess_save_path = $save_path;
echo "$session_name successfully opened<br>"; ////debug
return(true);
}
function close()
{
return(true);
}
function read($id)
{
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
return (string) @file_get_contents($sess_file);
}
function write($id, $sess_data)
{ echo "write begin<br>"; //////debug
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
echo "$sess_file<br>"; ////debug
echo "$sess_data<br>"; /////debug
if ($fp = @fopen($sess_file, "w"))
{
$return = fwrite($fp, $sess_data);
fclose($fp);
return $return;
}
else
{ echo "did not open file for writing<br>"; //////debug
return(false);
}
}
function destroy($id) // works in no case
{
global $sess_save_path;
$f = @fopen("./tmp/temp", "w"); /////debug
$sess_file = "$sess_save_path/sess_$id";
return(@unlink($sess_file));
}
function gc($maxlifetime)
{
global $sess_save_path;
foreach (glob("$sess_save_path/sess_*") as $filename) {
if (filemtime($filename) + $maxlifetime < time()) {
@unlink($filename);
}
}
return true;
}
session_set_save_handler("open", "close", "read", "write",
"destroy", "gc");
session_save_path("./tmp"); // temporary storage
session_name("counter");
session_start();
//$id = session_id(); //If I use these 3 strings, file will be created,
//$sess_file = "$sess_save_path/sess_$id"; // but would not be opened by write() all the
same.
//$fp = @fopen($sess_file, "w"); // file will be empty
if (!(session_is_registered("someVar"))) // new session
{ echo "someVar not registered<br>"; ////debug
session_register("someVar"); // session variable
$_SESSION["someVar"] = "someValue";
}
echo "it still works<br>"; ////debug
?>
File temp was not created.
A storage file was not created and output in browser was as follows:
counter successfully opened
someVar not registered
it still works
write begin
./tmp/sess_a4bc6c82ab15635de77bd0c9c74b13d4
someVar|s:9:"someValue";
did not open file for writing
Who could explain this phenomenom?
----
Server IP: 91.196.196.241
Probable Submitter: 77.123.23.74
----
Manual Page -- http://www.php.net/manual/en/faq.php
Edit -- https://master.php.net/note/edit/80413
Del: integrated -- https://master.php.net/note/delete/80413/integrated
Del: useless -- https://master.php.net/note/delete/80413/useless
Del: bad code -- https://master.php.net/note/delete/80413/bad+code
Del: spam -- https://master.php.net/note/delete/80413/spam
Del: non-english -- https://master.php.net/note/delete/80413/non-english
Del: in docs -- https://master.php.net/note/delete/80413/in+docs
Del: other reasons-- https://master.php.net/note/delete/80413
Reject -- https://master.php.net/note/reject/80413
Search -- https://master.php.net/manage/user-notes.php