[wp-hackers] Using php Sessions

Adam Backstrom adam at sixohthree.com
Fri Mar 30 04:32:01 UTC 2012


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


More information about the wp-hackers mailing list