D P.
D P.

Reputation: 1079

PHP session not working with multiple WordPress pages

Please help. I have three PHP pages and I have pasted the following code snippets on each using the “Woody snippets.” plugin. These sessions sometimes communicate within pages based on which page I published last. For example, if I publish page 1 last, I can see the session['rate'] values showing up on page 2 and page 3. But if I publish page 3 last and went into page 1 to populate session[‘rate’] and come back to page 3 again, page 3 will not have this value.

Page 1 code

<?php
session_start();
print_r($_SESSION);
$_SESSION['rate'] = 50;
echo "<br>".$_SESSION['rate']."<br>" ;
?>

Page 2 code

<?php
session_start();
print_r($_SESSION);
echo "<br>".$_SESSION['rate']."<br>";
$_SESSION['rate'] = 10; //This change wont effect on page 1 if I re-visit it. 
?>

Page 3 code

<?php
session_start();
print_r($_SESSION);
echo "<br>".$_SESSION['rate']."<br>";
?>

I have installed the “Native PHP Sessions for WordPress” plugin as well, which claims there is an issue with PHP sessions working properly with WordPress. But still no help. You can copy this code and past it into 3 different WordPress pages and you will still see the issue. (session[‘rate’] value is base on which page I publish last. I tried putting session_start(); on the header.php file on the appropriate theme file as well. Still no luck). Placing the following code on the function.php file doesn't help either:

function ses_init() {
   if (!session_id())
   session_start();
}
add_action('init','ses_init');

Later on, I want a user to stay logged in using sessions. But if sessions are no longer working properly with WordPress, this will not be feasible.

Upvotes: 3

Views: 2306

Answers (3)

LSerni
LSerni

Reputation: 57418

This is not, I think, a session issue. I had something very similar happen to me in a different kind of WP application I had to work with.

The basic reason is that WP takes great pains to optimize the connection, traffic, and rendering of pages. But if your three pages are "normal" pages accessed via GET method, then they will publish several metadata (ETag, If-Modified-Since header, etc.) that do not "know" that the content changed.

Without looking at the site I can't be sure whether this is your case, but I can tell you how I fixed my problem, which was similar in some ways to yours (I had some information updated in page A, that might have needed to be shown in page B of the same user, and the old version was shown instead).

Usually I would have fixed the caching pragmas or used a cache-buster, and maybe you can. My own case was slightly different - I did not describe it completely - and more complicated, so I could not do this.

What I did instead was way more complicated, but it worked perfectly and scaled very well. In your terms, I changed the code to

echo '<span id="rate"></span>';

and added a Javascript code to make an AJAX call that read the $_SESSION value and put it in the span:

jQuery.post('/utilities/rate.php').then(reply => {
    jQuery('#rate').text(reply.rate);
});

The rate.php file retrieved the session,

<?php
    // Use readonly session to improve performances and avoid locks
    if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
        session_start([ 'read_and_close' => true ]);
    } else {
        session_start();
        session_write_close(); 
    }
    $ret = [ 'rate' => $_SESSION['rate'] ];
    header('Content-Type: application/json;charset=utf-8');
    print json_encode($ret);

and this allowed me (this was another requirement) to update the information periodically (I used window.setTimeout in Javascript) even if the page was not reloaded, or the data changed server side, as soon as it changed.

update

Just to verify, try whether it's possible to send cache headers from the page -- and if they work.

session_start();
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0");
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");

confirmed

It is a cache of some type (from the headers I suspect "X-LiteSpeed Cache").

If I request the a1 page adding "?_=" and a random number to the URL, I get the updated page with the 50 rate value as expected.

It can probably be disabled site-wide, but then the site would become slow. There appear to be some headers that can be used to disable the cache for specific requests.

Try either

header('X-Litespeed-Cache_Control: no-cache');

or header('X-Litespeed-Cache_Control: no-store');

If not even this works, I'm really afraid you'll need to consider the AJAX solution, or an ugly cachebusting.

Upvotes: 1

Stefanpt
Stefanpt

Reputation: 77

Question: Why are you starting a new session on each page? What if you tried:

if (session_status() == PHP_SESSION_NONE) {
session_start();
$_SESSION['rate'] = 10;
}else{
echo "<br>".$_SESSION['rate']."<br>" ;
}

Failing that just write the values to cookie, its much easier

Upvotes: 0

Radolan
Radolan

Reputation: 129

I would advise on printing the session id on each page and make sure its the same, otherwise there is something wrong with your php session configuration, that is restarting the session every time instead of resuming it.

You can print the id using the following code:

<?php
session_start();
echo session_id();

Upvotes: 0

Related Questions