PHP Session Variable to WordPress Error

admin2025-01-08  4

I have been trying to figure out how to add session variables to WordPress Custom Pages for the past few days but have still been unable to find a solution. From researching it seems WordPress does not allow you to move Session variables from one page to the next. I have tried removing all of the 'session_start();' from each page and adding the below to functions.php file.

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

Have also tried adding code below to the wp-config.php but to no avail.

if (!session_id())
session_start();

The session will create a unique id for each user which will be checked on the next page to see if it equals the previous id. The first page code is as follows:

$_SESSION['t'] = md5(session_id().'3ac49262e797b6a51b6362e264d9dbe1');
session_write_close();

The next page is:

$testValue = md5(session_id().'3ac49262e797b6a51b6362e264d9dbe1');
if ($testValue == $_SESSION['t'])
{$passFlag = 1;}
else
{$passFlag = 0;}
session_regenerate_id();

'session_write_close();' is called further down on this page. Any help would be greatly appreciated and if you need any further information please don't hesitate to message. Thanks.

I have been trying to figure out how to add session variables to WordPress Custom Pages for the past few days but have still been unable to find a solution. From researching it seems WordPress does not allow you to move Session variables from one page to the next. I have tried removing all of the 'session_start();' from each page and adding the below to functions.php file.

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

Have also tried adding code below to the wp-config.php but to no avail.

if (!session_id())
session_start();

The session will create a unique id for each user which will be checked on the next page to see if it equals the previous id. The first page code is as follows:

$_SESSION['t'] = md5(session_id().'3ac49262e797b6a51b6362e264d9dbe1');
session_write_close();

The next page is:

$testValue = md5(session_id().'3ac49262e797b6a51b6362e264d9dbe1');
if ($testValue == $_SESSION['t'])
{$passFlag = 1;}
else
{$passFlag = 0;}
session_regenerate_id();

'session_write_close();' is called further down on this page. Any help would be greatly appreciated and if you need any further information please don't hesitate to message. Thanks.

Share Improve this question edited Aug 24, 2017 at 22:56 maxwell asked Aug 24, 2017 at 22:47 maxwellmaxwell 12 bronze badges 1
  • 3 WP doesn't use PHP sessions, and they won't play nicely with page caching systems. Also, some hosts don't support them because of the way they're built, e.g. WP Engine and PHP Sessions don't work together. Instead, rely on user meta, cookies, or client side local storage – Tom J Nowell Commented Aug 24, 2017 at 23:13
Add a comment  | 

2 Answers 2

Reset to default 0

You need to add your code to the functions.php file of your theme. (Actually, adding it to your Child Theme is best, since you don't want a theme update changing things. Unless you are writing your own theme.)

So this in your functions.php file:

if(!session_id()) {
    session_start();
}

(Making @Tom's comment into a proper answer)

PHP sessions are legacy from the beginning days of PHP when the use of DBs was rare, complex and expensive. Since then everybody moved on, and now there is nothing that a session can do that a DB will not do in a more consistent and reliable way. If you feel like you need some information stored on the server just write to the DB, whether it is to a user's meta or a new table (if your users do not map into WP users).

And since sessions require cookies in any case, you might as well just store your unique key in a cookie.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267846a1227.html

最新回复(0)