Zen Cart Logo
Forums / General Questions / Sharing PHP variables ($_SESSION question)

Sharing PHP variables ($_SESSION question)

Views: 1,209

Results 1 to 6 of 6
15 Oct 2014, 4:02 PM
#1
flyvholm avatar

flyvholm

New Zenner

Join Date:
Jul 2014
Location:
Denmark
Posts:
27
Plugin Contributions:
0

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?

15 Oct 2014, 5:03 PM
#2
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Sharing PHP variables ($_SESSION question)

flyvholm:

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.

flyvholm:

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

15 Oct 2014, 10:37 PM
#3
flyvholm avatar

flyvholm

New Zenner

Join Date:
Jul 2014
Location:
Denmark
Posts:
27
Plugin Contributions:
0

Re: Sharing PHP variables ($_SESSION question)

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

RodG:

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.

RodG:

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?

RodG:

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:

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?

16 Oct 2014, 4:08 AM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Sharing PHP variables ($_SESSION question)

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"?

16 Oct 2014, 6:16 AM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

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.

4 Nov 2014, 5:15 PM
#6
flyvholm avatar

flyvholm

New Zenner

Join Date:
Jul 2014
Location:
Denmark
Posts:
27
Plugin Contributions:
0

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!