We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Storing a Custom Session Array

Development and Programming

SiteGoals's avatar
SiteGoals
15 posts
15 years ago
SiteGoals's avatar SiteGoals

I’m building a site that features an extremely simple shopping cart. I want to avoid using Simple Commerce or any other plugin because there is no checkout or login process; the items in the cart just need to be saved as the person browses through the site.

I know how I would handle this with PHP sessions, but I know EE conflicts with those out of the box. I looked into the Session Variables plugin, but that doesn’t support arrays.

So what would be the recommended way of accomplishing this simple cart?

       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can
I know how I would handle this with PHP sessions, but I know EE conflicts with those out of the box.

Can you clarify this? I have used native PHP sessions in EE 1 and EE2 many times.

if (session_id() == '')
{
   session_start();
}

$_SESSION['my_array'] = array(
    'index' => 'value',
};
       
SiteGoals's avatar
SiteGoals
15 posts
15 years ago
SiteGoals's avatar SiteGoals

You’re right, I totally forgot to include session_start(). But, now I’m having trouble passing the information between all my various templates.

I’ve got index.html which has {embed="core/.header"} and {embed="core/.footer"}. In header.html I’m accessing $_SESSION and attempting to put it into a variable. Unfortunately footer.html doesn’t see any of that.

Here’s the code in header:

$string = array();

foreach($_SESSION['quote']['bottles'] as $key[0]){
    $string[] = $key[0][0];
}

$string = implode("|", $string);
$EE->session->cache['string'] = $string;

Here’s the code in footer:

<? echo $string; ?>
       
SiteGoals's avatar
SiteGoals
15 posts
15 years ago
SiteGoals's avatar SiteGoals

No matter what I do, I cannot figure out how to store an array across all pages and templates. I’ve tried using $_SESSION, $this->EE->session, standard $php variables. Is there a secret that I’m missing or is this just not possible in EE?

       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can

Are you calling this:

if (session_id() == '')
{
   session_start();
}

on each page?

       
SiteGoals's avatar
SiteGoals
15 posts
15 years ago
SiteGoals's avatar SiteGoals

I’m calling it in the header, which is called on each page via an embed. The session variables show up in the header, but not on any of the pages the header is embedded on.

       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can

This is a parse order issue. Call the session_start() at the top of each template and not in an embed. Embeds are parsed after everything in the main template.

       
SiteGoals's avatar
SiteGoals
15 posts
15 years ago
SiteGoals's avatar SiteGoals

Thank you the3mus1can! You were right about the parse order and it appears to be working correctly now.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.