Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 245463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:05:27+00:00 2026-05-11T21:05:27+00:00

I’m trying to write a custom session persister in PHP. But for some reason

  • 0

I’m trying to write a custom session persister in PHP. But for some reason only my session read functon is getting called, not my session write function. Thus the session isn’t persisted. If I switch back to the default session handler (file), everything works. If I add session_write_close() at the end of the test script – everything works. But PHP manual explicitly states that you do not need to explicitly call the session_write_close() because it gets automatically called at the end of the script. And I don’t want to put session_write_close() at every possible exit point in my existing application!

Here’s some code that illustrates the problem. I’m running it on WinXP SP3 + Apache 2.2.9 + PHP 5.2.6. The log file only contains entries for s_open() and s_read().

<?php
function logger($str)
{
    file_put_contents('session_log.txt', date('Y-m-d h:i:s') . " $str\r\n", FILE_APPEND);
}

function s_open()
{
    logger('s_open');
    return true;
}

function s_close()
{
    logger('s_close');
    return true;
}

function s_read()
{
    logger('s_read');
    return @file_get_contents('session.txt');
}
function s_write($id, $data)
{
    logger("s_write: $id - $data");
    file_put_contents('session.txt', $data);
    return true;
}

function s_destroy()
{
    logger('s_destroy');
    return true;
}

function s_gc()
{
    logger('s_gc');
    return true;
}

session_set_save_handler('s_open', 's_close', 's_read', 's_write', 's_destroy', 's_gc');

session_start();

if ( isset($_SESSION['time']) )
    echo 'Session time: ' . date('Y-m-d h:i:s', $_SESSION['time']);
else
    echo 'Session empty';
echo '<br>';    
echo 'Session ID: ' . session_id();


$_SESSION['time'] = time();
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:05:27+00:00Added an answer on May 11, 2026 at 9:05 pm

    Blargh! Got caught on a notice that PHP itself had put there! To quote the manual:

    Warning

    Current working directory is changed with some SAPIs if session is closed in the script termination. It is possible to close the session earlier with session_write_close().

    It was indeed changing and the calls to s_write() and s_close() were logged in another directory!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What do you define to be success? If you define… May 12, 2026 at 10:55 am
  • Editorial Team
    Editorial Team added an answer regex is not the tool for the job, you will… May 12, 2026 at 10:55 am
  • Editorial Team
    Editorial Team added an answer At the moment there are no browsers that actually care… May 12, 2026 at 10:55 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.