473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Falling back to PHP's session handler

Hi all,

I've created a custom session handler that saves session data to a
MySQL database. If the database server is down, I'd like to fallback to
PHP's default session handler (files) until the server comes back up.

After calling session_set_save_handler(), is there a way to tell PHP to
ignore this setting and revert to temp files for session storage? If I
try an ini_set on session.save_handler upon detecting that the DB
server is down, it doesn't work because the session is already active.

Thanks

Mar 14 '06 #1
3 2209
us****@shat.net wrote:
Hi all,

I've created a custom session handler that saves session data to a
MySQL database. If the database server is down, I'd like to fallback to
PHP's default session handler (files) until the server comes back up.

After calling session_set_save_handler(), is there a way to tell PHP to
ignore this setting and revert to temp files for session storage? If I
try an ini_set on session.save_handler upon detecting that the DB
server is down, it doesn't work because the session is already active.

Thanks


Try shutting down the session by calling session_write_close() before
you call ini_set(). The update handler on session.save_handler checks
the session status. Resetting the status to inactive should allow you
to have the handler back to "files."

I wonder how well such a fallback mechanism would work. Active sessions
at the moment when the database goes down would be lost. The same thing
would happen when the database comes back online as the session files
are now ignored.

Mar 14 '06 #2
This did the trick, though I discovered along the way that PHP is
extremely picky about when the call to session_write_close() is made.

When the database goes offline or comes back, the active sessions will
be lost, but I'm trying to plan for the "3AM contingency." Suppose the
DB goes down for an hour, I'm not too worried about having a handful of
sessions drop at the onset, I'm more interested in making sure that
everyone else's sessions work during the downtime.

Thanks again :)

Mar 14 '06 #3
us****@shat.net wrote:

I've created a custom session handler that saves session data to a
MySQL database. If the database server is down, I'd like to fallback to
PHP's default session handler (files) until the server comes back up.

After calling session_set_save_handler(), is there a way to tell PHP to
ignore this setting and revert to temp files for session storage? If I
try an ini_set on session.save_handler upon detecting that the DB
server is down, it doesn't work because the session is already active.

You're trying to solve the wrong problems in the wrong way.

Unless you've got a very unusual application, the biggest problem you are
going to have is detecting the state of the DBMS. You don't want to be
waiting for every database operation to timeout. This then solves your
problem but shifts the emphasis onto a reliable method for detecting an
outage.

Since the interval between reading and writing a session *should be* a lot
shorter than the interval between writing and reading a session, there
there will be very little chance of rescuing session data during the
failure. So don't waste your time trying to change the save handler after
you've set it up. Again this makes your life simpler, although its possibly
not the news you wanted to hear.

This just leaves a simple if wrapper around the session handler:

if ((file_exists($database_working_semaphore)) || (rand(0,100)>98)) {
if (test_db_connection()) {
file_put_contents($database_working_semaphore, time());
} else {
unlink($database_working_semaphore);
}
}
if (file_exists($database_working_semaphore)) {
// use database session handler
} else {
// use file handler
}

Now if you are wanting a fault-tolerant solution instead of a failover
solution....that's a different story. But you'll have to raise your game a
lot. (ouch ! mixed metaphor).

C.

Mar 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Bryant | last post by:
I'm trying to extend an existing site written in CF. We're going to move it to PHP at some point in the near future, but in the meantime, I'd like to add some minor new features, and can steal...
2
by: Terence | last post by:
Does anyone have any experiences to share regarding this? we run a ColdFusion/PHP/Java(cocoon) environment and I'll probably figure out a way for all apps to be able to share sessions. I wanted...
2
by: somaboy mx | last post by:
I'm working on a site which contains a lot of legacy ASP VBScript code. My intention is to recreate and expand the site's functionality in PHP5. I'm doing this in phases, so in the meantime there...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
3
by: Justin Koivisto | last post by:
I need to do some testing for an IIS server (not my choice!)... So I installed IIS on my WinXP Pro machine (all updates applied), got php 5.1.1 installer (& zip for all the pear stuff), and got...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
by: Karl Nierler | last post by:
Hi everybody, I am currently developing my first commercial (customer specific) PHP application. This application is in fact nothing else than a content management system with internal...
4
by: Christoph | last post by:
A while ago, I implemented an AJAX-based progress bar on a web page that executed a lengthy server operation. The web page was a Java servlet on a Tomcat server. The progress bar worked by...
5
by: Gilles Ganault | last post by:
Hello Out of curiosity, I was wondering what happens with session data: I read that session data are saved on disk as eg. /var/sess_ID (which is already a performance issue since accessing a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.