Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2014
    Location
    Denmark
    Posts
    27
    Plugin Contributions
    0

    Default Sharing PHP variables ($_SESSION question)

    I'm writing a PHP script to communicate with Amazon for their fulfillment service. The script needs to access information that Zen Cart stores in the $order variable.

    Q1) How does Zen Cart preserve and make a variable like $order accessible across pages?

    I'd prefer using the $_SESSION variable to pass the info. In comparison, extracting the necessary info and passing via GET seems quite cumbersome. Unfortunately, adding session_start() to my script gives me a new, empty $_SESSION variable. I call the PHP script via jQuery from the file tpl_modules_shipping_estimator.php.

    Q2) Any idea why session_start() creates a new session instead of resuming?

  2. #2
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    6,167
    Plugin Contributions
    7

    Default Re: Sharing PHP variables ($_SESSION issue)

    Quote Originally Posted by flyvholm View Post
    I'm writing a PHP script to communicate with Amazon for their fulfillment service. The script needs to access information that Zen Cart stores in the $order variable.

    Q1) How does Zen Cart preserve and make a variable like $order accessible across pages?
    Either via the GLOBALS (classes) or the $_SESSION variables.

    Quote Originally Posted by flyvholm View Post
    Q2) Any idea why session_start() creates a new session instead of resuming?
    Because session_start() literally starts a new session. Clearly this isn't what you want since ZenCart already has a session started.

    At the start of you module functions add

    global $order ;

    Retrieve values using code like:

    $myPostcode = $order->delivery['postcode']

    Cheers
    RodG
    Last edited by RodG; 15 Oct 2014 at 06:10 PM.

  3. #3
    Join Date
    Jul 2014
    Location
    Denmark
    Posts
    27
    Plugin Contributions
    0

    Default Re: Sharing PHP variables ($_SESSION issue)

    Thanks, Rod, appreciated. I still don't have the details straight though.

    Quote Originally Posted by RodG View Post
    Because session_start() literally starts a new session.
    In my case, yes. But most advice I'm able to find just puts a session_start() at the top of a page and the previous session is automatically resumed (such as here). What's unclear to me is how session_start() determines whether to resume a session or start a new one.

    Quote Originally Posted by RodG View Post
    Either via the GLOBALS (classes) or the $_SESSION variables.
    Each page load in Zen Cart includes a session_start() to resume the session and hence restore $_SESSION, so I can see how that works. For what I know that doesn't restore other superglobals, however, so how is info in $GLOBALS restored?

    Quote Originally Posted by RodG View Post
    At the start of you module functions add

    global $order ;
    Unfortunately that does not work in my case. I'm using jQuery/ajax to retrieve info and update the shipping table based on the zip code entered. For what I can tell that requires the PHP script (sending a query to Amazon) to be in a separate file, and it can't see the Zen Cart variables - including superglobals.

    A solution I thought would do the trick is to pass the session ID (retrieved with session_id() in tpl_modules_shipping_estimator.php) to my PHP script via the ajax GET call. I then start my script with:
    PHP Code:
    session_id($_GET['id']);
    session_start(); 
    That seems to work for others; in my case session_start still won't resume the session (i.e. $_SESSION is empty). I've verified that $_GET['id'] in my script does match the output of session_id() in tpl_modules_shipping_estimator.php. What am I doing wrong?

  4. #4
    Join Date
    Jul 2012
    Posts
    16,735
    Plugin Contributions
    17

    Default Re: Sharing PHP variables ($_SESSION issue)

    Have looked a little more myself and does seem that the recommendation is touse session_start() on each page that the session information is to be used.

    One thing I see within the php manual is that the session information should be provided to the next aspect, not retrieved from if that makes sense. ZC uses a $_POST variable (hidden) to store the session I'd after the first session identifier is in the get of the uri... Seems like the session id (SID) is not making it to the next step or is being cleared by something... Have you looked at: http://php.net/manual/en/function.session-start.php for any possible resolution? I see a link created by appending the SID is used to pass the session id.

    Perhaps posting the three "files"?
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  5. #5
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Sharing PHP variables ($_SESSION question)

    Really, why bother with starting sessions all over again and trying to re-read session data at all?
    If the data is static (and I don't see why it wouldn't be in your case), why not just json_encode() whatever data you REALLY think is needed (as little as necessary and NO MORE, for security reasons) and output that into a javascript variable into the page (in your case you said you're doing it with shipping-estimator for some reason, so do it there), and then your jQuery/Ajax/whatever can simply read that data directly from the browser and act on it as needed.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  6. #6
    Join Date
    Jul 2014
    Location
    Denmark
    Posts
    27
    Plugin Contributions
    0

    Default Re: Sharing PHP variables ($_SESSION question)

    Sorry for the delay, but thought I'd close this up... I never figured out what the problem was starting the session because DrByte is (of course) right. Doing a session start is a big waste considering the little data I need to transfer. I'm learning as I go and just wasn't aware of json_encode() and how easy it is transferring the data using that. So in the end it was for the better I could not get the session start working... Thanks DrByte for pointing me in the right direction!

 

 

Similar Threads

  1. setting $_SESSION variables in an Observer Class
    By tcarden in forum Contribution-Writing Guidelines
    Replies: 1
    Last Post: 13 Feb 2013, 06:37 AM
  2. Passing variables without $_SESSION
    By fred_tededmondsson in forum General Questions
    Replies: 1
    Last Post: 31 Oct 2011, 07:25 PM
  3. Replies: 1
    Last Post: 21 Apr 2010, 06:37 PM
  4. Replies: 3
    Last Post: 10 Apr 2009, 10:37 PM
  5. where are $_SESSION variables set in order process
    By apathy in forum General Questions
    Replies: 0
    Last Post: 30 May 2008, 01:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR