最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - $_SESSION variables work perfectly on my local machine but returns NULL on development webserver

programmeradmin1浏览0评论

I'm using a hook into init where I add a certain timestamp in order to include a splash screen into within my Wordpress project.

Basic logics of the script is:

  • add_action("init", "my_function", 1)
  • my_function handles all the logic and stores the $_SESSION variables I need else where

When I try to check the variables on my local machine (MAMP, Mac) it works perfectly fine. I can get the timestamp and simply check if the timestamp + offset is greater than the current time. If / else I will show the screen.

I pushed the scripts to my online development server however and for some kind of reason the $_SESSION variable returns NULL entirely. I've read things about destroying the session after usage, but that doesn't really work.

Wondering, does anyone know what's wrong here? I honestly have no idea... Besides; this whole chapter is very poorly documented on Wordpress' side which leaves me no other choice than asking here...

I'm using a hook into init where I add a certain timestamp in order to include a splash screen into within my Wordpress project.

Basic logics of the script is:

  • add_action("init", "my_function", 1)
  • my_function handles all the logic and stores the $_SESSION variables I need else where

When I try to check the variables on my local machine (MAMP, Mac) it works perfectly fine. I can get the timestamp and simply check if the timestamp + offset is greater than the current time. If / else I will show the screen.

I pushed the scripts to my online development server however and for some kind of reason the $_SESSION variable returns NULL entirely. I've read things about destroying the session after usage, but that doesn't really work.

Wondering, does anyone know what's wrong here? I honestly have no idea... Besides; this whole chapter is very poorly documented on Wordpress' side which leaves me no other choice than asking here...

Share Improve this question asked Nov 7, 2019 at 11:51 JimJim 1601 silver badge12 bronze badges 1
  • It's not documented on the WP docs because WP doesn't use PHP sessions, and neither should you – Tom J Nowell Commented Nov 7, 2019 at 12:02
Add a comment  | 

1 Answer 1

Reset to default 1

WordPress does not use $_SESSION variables, and neither should you.

Sessions variables come with a lot of issues:

  • they're fundamentally incompatible with page caching plugins
  • they're incompatible with systems such as Cloudflare
  • WordPress uses cookies to track sessions
  • a lot of WP hosts are not set up to work with PHP sessions variables, e.g. WP Engine

If your goal is to show a splash screen the first time people visit your site, you don't need PHP session variables, there are much better/simpler/easier ways to do it.

For example:

  • Cookies!! This is probably your best bet, and can be set via javascript too
  • Using Javascript, and relying on a local storage mechanism
  • There may even be a CSS based solution using the :visited pseudo selector, e.g. .modal_link:visited * { display: none; visibility: hidden; } where modal_link is a class name on a hyperlink to your homepage
  • not showing a splash screen as it's awful UX and incredibly annoying to your sites visitors, and if you accidentally dismiss it, you need intermediate to advanced developer skills to bring it back. Research shows numerous disadvantages, etc, etc, etc

The last option would provide the best user experience.

As for your local machine, MAMP etc can't make too many guesses about what it's being used with, unlike a WP managed host. It's also extremely unlikely a CDN has been inserted between you and MAMP. Any page caching is probably disabled for logged in users as well. That doesn't mean what works in MAMP should work everywhere, just that what works in MAMP will work in MAMP

发布评论

评论列表(0)

  1. 暂无评论