user6096423
user6096423

Reputation: 141

Make Sessions work with WordPress

Normally this works:

page 1

  <? session_start();

   $_SESSION['note']  = "hello";

 ?>

page 2

<? session_start();

   echo $_SESSION['note'];

?>

Navigate to Page 1 to start the session and load the variable. Navigate to page 2 and the variable is alive. Easy.

Now I come to WordPress.

WordPress has a 'theme' in a particular folder, with its own header and footer. The 'template', so to speak.

To create a page, you login to the admin section, click add page and you are greeted with a blank screen. If you type nothing, and 'publish' all you will see is the header and footer.

That means, any php coding that is page specific is entered onto the blank text box, which when displayed is sandwiched in between the default 'theme' header and footer.

So, it is in this section I enter page 1 (same as above):

<? session_start();

   $_SESSION['note']  = "hello";

?>

then page 2 (same as above):

<? session_start();

   echo $_SESSION['note'];

?>

Result? Nothing.


So this is what I tried:

Installing a wp plugin called WP sessions manager.

After installing the plugin, and trying the code as above, which did not work, I tried this syntax p1:

<? $wp_session = WP_Session::get_instance();

   $wp_session['note'] = "hello";

 ?>

and this p2

<? $wp_session = WP_Session::get_instance();
   $wp_session['note'] = "hello";
?>

Result? Still nothing.

I tried placing the same code at the top of the index and header pages for the theme, in the theme folder and the primary folder, but nothing works.

Since the session works fine with standalone php pages, something to do with wordpress is blocking the session, keeping it from working, and I am trying to figure out why and fix it.

Upvotes: 0

Views: 827

Answers (1)

Reza Saadati
Reza Saadati

Reputation: 5429

Use:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Now, the first thing that your theme should do is session_start(). Put it just on the top of header.php or on the top of index.php. And right after that use:

die(var_dump($_SESSION));

If that doesn't work, you might have a problem with your theme.

Check your theme to see if sessions get destroyed somewhere. Search for session_destroy().

Upvotes: 2

Related Questions