7

I'm writing a WordPress plugin to deal with a third-party API.

This API requires a session identifier be passed to it so that the API (which basically functions as a third-party cart) knows what item to add where - again, think of it like a cart.

I want to store this session identifier in the sessions of my WordPress users. The idea would be to connect to this API, request the session identifier, and then store it in the WordPress/PHP session so I can reuse it as they navigate around the site and add more items to their third-party cart. I recognize this is not ideal (why not use WooCommerce to do this directly?) but we need some additional functionality and data that only this third-party can provide.

Anyway, as I went about trying to do this, I found this from our host : https://wpengine.com/support/cookies-and-php-sessions/

Which I confess I find terribly confusing.

According to that, PHP sessions and functions like session_start() and session_id() are not advisable, and won't work on this host - ...our system specifically ignores headers defining a PHPSESSID cookie.

But as the docs there point out - IF PHP SESSIONS ARE A TYPE OF COOKIE, WHAT’S THE PROBLEM?

In my mind, these docs don't answer that question.

It says:

The biggest conflict when using PHP Sessions has to do with the unique session identifiers. Because every user’s identification string is unique, this “busts cache” with every new user to visit your site. This kind of a system will not scale with many concurrent site users! With that in mind, our system specifically ignores headers defining a PHPSESSID cookie.

My question here is, isn't this true of the sessions they advocate, which are done via merely a different cookie which then leads to a database connection?

They say here:

The reality is, there’s a better way to store user session data! The correct method is to use the database to store session data. WooCommerce and other eCommerce solutions have already converted to using this method over traditional PHP Sessions, and also use their own cookie naming conventions.

These docs go on:

Beyond the performance and scaling implications, PHP Sessions present other problems. By default, PHP Sessions store data to the filesystem as their own unique file. Writing data to a file is an I/O process, and these kinds of processes are known to back up and cause high server load if your site gets too many concurrent users. Not to mention, this kind of session storage simply doesn’t work if your site is on a clustered solution spanning multiple web servers.

Why is this any worse/better than storing the sessions in a database? Isn't that also I/O?

I'm trying to understand - why is the default PHP session considered bad and the WooCommerce session considered good? I've looked into the WooCommerce source code on sessions, but I am not experienced enough in this arena to decipher why what they are doing avoids vulnerabilities as mentioned in the host's docs.

Specifically:

There are multiple security vulnerabilities centering around PHP Sessions. Vulnerabilities include Session data being exposed, Session fixation, and Session hijacking.

How does WooCommerce mitigate this where PHP sessions do not? What is advisable for my use case?

Obviously, I don't want insecure sessions, but developing a session manager/cookie/database connection, as Woo seems to have done, would take some time. So I'm trying to understand why/if this is necessary to replicate before I invest the time into avoiding PHP sessions.

1
  • 1
    I ran into a similar requirement with WP Engine before and they were able to whitelist a custom session variable and cookie I needed for custom development through their support. Might be worth reaching out. Mar 27, 2018 at 18:03

1 Answer 1

2

This has very little to do with wordpress, but if this answer will help eliminate the usage of session, it might be worth it....

Sessions are stored on the hard disk of the server. This is problematic in a shared hosting scenarion because the session directory is most likely readable and writable by all process running on the server, not only your site. All shared resources on a shared hosting server that do not have OS level user based protection (like directory permissions) is at least suspicious and you should not use it for anything secure or private.

lets repeat Sessions are stored on the hard disk of the server. What happen if you are running in a multi server enviroment? How will you be able to locate your session information. The only way to have it work is to tie users to specific servers which is not optimal.

And again Sessions are stored on the hard disk of the server. If you use sessions as integral part of your HTML generation it means that you can not cache your HTML.

WPEngine, is a shared hosting in a multi server enviroment that does caching by default.

As for WC, as it said in what you are quoting, they do not use php sessions, and invent their own mechanism by storing session information in the DB. This is also a problematic solution as it means that heavy traffic might bring the site down if you do not do it with extreme care (I guess in that case the session starts only when a user starts the process of buying a product). Why they do it like that and do not rely on cookies, you will have to ask them, I assume that tracking incomplete transactions is part of it, another part is that cookie information on http sites is basically public, and if you want to avoid information leakage you need to store it on the server.

What is adviced in your case? this will be hard to say without more details, but probably you should work to eliminate the use of anything which feels like a session. HTTP was not designed to have sessions, and it is best not to fight this fact of life.

4
  • Thanks for your answer! Just to be clear, WooCommerce does rely on cookies - it just hides away most of the session info in a database rather than a file - docs.woocommerce.com/document/woocommerce-cookies Mar 21, 2018 at 15:40
  • I meant that they use the session storage only when they have to (that is what I assume), and not to track every random visitor, as WC sites are likely to be heavily cached. Mar 21, 2018 at 16:11
  • With PHP, it's possible to configure sessions to be stored on the filesystem, or in a database table. There was no need for WordPress to re-invent the wheel here, but there you go
    – MrCarrot
    Mar 10 at 9:56
  • sure, every request will have at least one DB read and many of them one DB write before even doing any thing. Yes you can use NFS as file system that can be accessed by multiple servers for storing session files or memcache, but that still requires network activity which will slow your site down. Session files are a lazy solution from PHP 3(?) time, so mabye time to move forward and build software like its 2023. Mar 10 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.