Re: Sessions, Constructors, and Destructors - Oh my!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dan Trainor wrote:
I was doing some thinking today about the above three subjects.  Now, I
might sound like a complete tool here because I don't think I quite know
exactly in which instances constructors and destructors can be used -
but what about inside a session?

Say I had a visitor hit a site.  A session would start, and the
constructor would preform some housekeeping with the session;  setting
data correctly, gathering other data, executing another PHP function,
yada yada.  Then, when the session expired, Mr. Destructor would come in
and clean up - from within the session.

Is this possible?  Does it work this way?  If so, or if not for that
matter, please help me out here to better understand how these three
elements interact with eachother, if at all.

I think that (in PHP5 -- PHP4 doesn't have real destructors) you could put an object inside the $_SESSION variable and it would be serialized in the session (as long as you had the class definition available when it was unserialized, i.e. before session_start() is called).

However it's really being constructed and destructed with each request. You could do some __sleep and __wakeup magic, possibly [1].

Also - how would one go about handling sessions behind a load-balancing
configuration?  The best I've thought of is to use some sort of load
balancer which also has an NFS share.  Sessions are created with this
load balancer, and Apache or whatever proxy's the connection to the
machines behind the load balancer.  The machines behind the load
balancer map the NFS share from the load balancer, and are able to
interact with the session.  I'm very curious as to how session tracking
is done through multiple machines, as well.

The best thing to do in this situation is to write your own session_save_handler that uses a database, and point it at the MySQL server [2]. If you really had to, you could maybe put the session_save_path on the NFS share [3].

Both the MySQL server and NFS share would be shared by multiple Apache machines (although the MySQL "server" may actually be a cluster if you're doing master-slave replication, that doesn't really matter in this case -- just send the writes to the master and the reads to any old slave), and so the session would be shared across all the machines, regardless of whether they get a different Apache every request.

[1] http://www.php.net/manual/en/language.oop5.magic.php
[2] http://www.php.net/session_set_save_handler
[3] http://www.php.net/session_save_path

--
Jasper Bryant-Greene
Freelance web developer
http://jasper.bryant-greene.name/

If you find my advice useful, please consider donating to a poor
student! You can choose whatever amount you think my advice was
worth to you. http://tinyurl.com/7oa5s

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux