[wp-hackers] Using php Sessions

Matt Slocum mattslocum at sharefaith.com
Fri Mar 30 20:37:08 UTC 2012


Thanks Adam and Kurt.

I also did some reading and found
http://www.php.net/manual/en/function.session-write-close.php. If you close
the session once you are done with it. It will release the lock, and allow
for another request from the same user to access the session data while the
other request is still processing. One would still have to be careful to
avoid out of sync read/writes like Kurt said.

Maybe I misunderstand transients, but I believe transients are site wide
and not session based. I'm looking to have every person browsing able to
have unique data.

After re-thinking my logic, I was able to easily move the logic to the init
hook before headers are sent. I am now just using a simple cookie and a
global variable, so I don't need to use sessions.

Thanks,
Matt


On Thu, Mar 29, 2012 at 10:50 PM, Kurt Payne <
kpayne+wordpress+hackers at gmail.com> wrote:

> > only one script can open a given session at a time.
>
> To clarify: only one php process can save session data for a specific
> session at a time.  This is because the file based session handling
> uses file locking.
>
> > Not a problem if you *only* ever load pages sequentially,
> > but add some latency to the mix or start loading rich data
> > via ajax and you compound the problem.
>
> This is true.  The latency for sequential requests is a safety net, so
> weigh the options and choose what's best for your site.
>
> A handler that doesn't lock the session for read+write can result in
> race conditions that will cause your session data to be out of sync.
>
> request 1 -> read session
> request 2 -> read session
> request 1 -> write session
> request 3 -> read session <-- What request 1 wrote
> request 2 -> write session <-- request 3 and 2 now out of sync
> request 3 -> write session <-- request 2 data now lost
>
> --Kurt
>
> On Thu, Mar 29, 2012 at 9:32 PM, Adam Backstrom <adam at sixohthree.com>
> wrote:
> > On Thu, Mar 29, 2012 at 20:30, Matt Slocum <mattslocum at sharefaith.com
> >wrote:
> >
> >> Hi,
> >> I'd like to keep track of some visitor's data while they browse my site
> and
> >> modify some navigation options based on their behaviors. I could do this
> >> using php's set_cookie() and store some info and make the desired
> >> modifications. Using cookies is somewhat iritating because I have to do
> my
> >> logic that I want included into the cookie before the headers are sent.
> >> I've done other PHP programming on non-wordpress sites and I normally
> use
> >> SESSIONS. I know that WordPress does not use sessions by default. It
> would
> >> be nice to use them since I could change some of the session data after
> the
> >> tracking cookie has been sent, and it would still persist because of the
> >> nature of sessions.
> >>
> >> Is there any reason that I shouldn't use php sessions on my site?
> >>
> >> Both styles would send another cookie. I guess non-session style would
> use
> >> fewer system resources. Is that the only reason?
> >
> >
> > If you're using PHP's default file-based session storage, only one script
> > can open a given session at a time. Subsequent scripts have to wait until
> > the first releases the lock on the session file. Not a problem if you
> > *only* ever load pages sequentially, but add some latency to the mix or
> > start loading rich data via ajax and you compound the problem.
> >
> > I'm not up to date on the internal workings of Transients (read "this
> > suggestion might be inefficient"), but that's one WordPress facility you
> > could use for storing temporary user data. Set a cookie early in the
> > request, read and modify a transient as necessary, possibly after headers
> > are sent.
> >
> > http://codex.wordpress.org/Transients_API
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list