Are WP transients specific to the user-session or can they be accessed from any session anywhere?
Since they are written to the DB, I would imagine they are public and accessible everywhere, but is there a way to limit their scope to only one user’s session?
The website I am building does not offer a login/memmbership option. By user I mean anyone that is viewing the website – I’m not talking about wp_users
!
Example:
-
I load the home page which generates a random number and stores it into a transient which should be accessible within that same session.
-
However, even from the same computer, but from a different browser, or different IP, I should not be able to access this transient…
Is this possible?
Transients are just database keys that expire. It’s like telling WordPress you want it to remember a certain piece of information, but for a limited time. In general, transients are accessible to PHP through any request.
But since they’re server-side, transients are only exposed to front-end users if you as the developer expose them.
A Solution
Given your specific question:
I would recommend pairing Transients with cookies. Basically, generate a unique key for every request (perhaps a hash of
time()
and some randome number) and use that as the user’s session key. Then, you can store a transient for just that user. Be sure to pass this unique key to the browser in the way of a cookie, and the next time that same user refreshes the page, they’ll get the same number.To give you some untested pseudo-code …
In plain English, this will:
wpa_105249_key
cookie in the requestAny code you call after this – basically anywhere else in your code – you can grab the current user’s random number simply by calling:
Transients are persistent storage mechanism. That means that by saving transient with certain key you will be able able to retrieve same transient with that same key (if it had not expired and cache have not been flushed since).
“Public” does not really apply to them, since they are internal code concept and only thing that accesses them is your code, not user’s browser.
If you want to store user-specific data in transient it will probably be challenging to come up with user-specific keys for it.
Storing information in user’s cookies or using some kind of proper session mechanism will probably be a better fit.