Forums / Customization from the Admin / referencing $_SESSION['customer_id'] variable outside of ZenCart

referencing $_SESSION['customer_id'] variable outside of ZenCart

Views: 11,729

Results 1 to 20 of 20
21 Nov 2006, 2:55 PM
#1
Join Date:
Mar 2005
Posts:
24
Plugin Contributions:
0

referencing $_SESSION['customer_id'] variable outside of ZenCart

Hi,

Is it possible to use the value of the $_SESSION['customer_id'] variable in pages outside of ZenCart?

When I try - the value isn't available.

Is it something to do with the scope of the variable?

Is there anything I can do to reference it from pages outside of ZenCart please?

Thanks,

Andy

21 Nov 2006, 3:57 PM
#2
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

If you are seeking this in an attempt to use ZenCart functions for an html based site where you only want the add to cart and checkout fnctions and maybe others.

This is not possible as ZenCart is not modular, and you have stumbled upon one of the major issues with trying to do this and this is sessions. For security reasons these are not static and therefore cannot be normally resused one the main session has expired.

If you are seeking to display some specifics on another site, look at a couple of modules in the downloads area there is one for inclusion in html and I think that ideagirl is the author and another for a php page.

Zen-Venom Get Bitten

21 Nov 2006, 6:15 PM
#3
blindside avatar

blindside

Totally Zenned

Join Date:
Sep 2004
Posts:
1,237
Plugin Contributions:
1

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Expanding on Kobra's comment, $_SESSION variables are specific to a user's session, which is a unqiue visit to the site and identified by a session id. It's not possible to use these variables on a static page without first establishing a session.

Furthermore, session variables are limited to the domain (and sub-domain, if specified) in which they were created. This is a general web restriction for security, as sharing session variables across sites opens the possibility for hackers to steal a session and assume a person's identity on a site.

That being said, there are ways around session limitations, but they might open up security holes as described above. You should read up on $_SESSION and the various session functions in the PHP manual to gain a better understanding.

If you only seek to use some of Zen's functions (and not any info in the DB), you could mimic the InitSystem, loading the stuff the want at page load, and bypass the session stuff.

Frank Koehl
"Cleverly Disguised as a Responsible Adult"

frankkoehl.com

21 Nov 2006, 9:20 PM
#4
Join Date:
Mar 2005
Posts:
24
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

I'm not trying to use ZenCart functionality in a static html page. I'm using PHP pages and am familiar with PHP sessions.

I need to get hold of the 'customer_id' session variable as I want to provide other facilities to customers whilst they have logged in to the site. I would like them to be able to update a user profile but I don't want them to have to login again if they've already logged into ZenCart. I would therefore like to use their 'customer_id' to record their profile by.

The PHP pages I would like to use the 'customer_id' within are part of the same site as the ZenCart shop.

So - can you tell me how I reference the session variable please within these other PHP pages?

Thanks,

Andy

22 Nov 2006, 4:10 AM
#5
blindside avatar

blindside

Totally Zenned

Join Date:
Sep 2004
Posts:
1,237
Plugin Contributions:
1

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

You could pass it via a form using POST or GET, just insert the value of $_SESSION['customers_id']. I do this on two sites where I link users' login session. I use a special link, pass the necessary variables, and perform some verification on the other side.

Frank Koehl
"Cleverly Disguised as a Responsible Adult"

frankkoehl.com

22 Nov 2006, 9:05 AM
#6
Join Date:
Mar 2005
Posts:
24
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Yes but that would require them to click on a link or a button after they have logged in wouldn't it to pas the values within the POST?

I just want to be able to reference the variable in a page that is outside of ZenCart at some point when they choose to accesss it in the session.

I've tried using $_SESSION['customer_id'] as the variable in the page outside ZenCart but the value assigned to it isn't available.

Is it a scope issue?

Andy

22 Nov 2006, 2:53 PM
#7
blindside avatar

blindside

Totally Zenned

Join Date:
Sep 2004
Posts:
1,237
Plugin Contributions:
1

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Yes, which is what myself and others were trying to explain; it's also why I used the "magic link" setup. $_SESSION is only available within the current session and defined domain.

If you are staying within the current domain, make sure you are actually starting the session on your non-Zen page. Even when a session already exists, you must instruct PHP to continue the session on your page. Zen Cart take care of this through the stuff going on in application_top.php, so it appears invisible to you. On a custom page, you need to at least make a call to session_start() (link to function details).

Again, do some reading. The PHP manual is invaluable for stuff like this.

Frank Koehl
"Cleverly Disguised as a Responsible Adult"

frankkoehl.com

22 Nov 2006, 3:41 PM
#8
Join Date:
Mar 2005
Posts:
24
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Hi Frank,

Thanks for your help.

However - I'd like to let ZenCart look after the logging in of the user. The session variable is therefore initially created 'within' a ZenCart page isn't it rather than outside it s you mentioned?

My PHP page outside ZenCart does use the following lines at the top of the page:

session_cache_limiter('must-revalidate');
session_start();

Out of curiosity - are 'you' able to reference the value of the 'customer_id' session variable in a page outside ZenCart on your web site?

Andy

23 Nov 2006, 1:04 AM
#9
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Just got back to this...

If your user has logged in I will assume that they are in the cart. How are they to get to this page you reference? As stated, if you generate the link to the other page from within ZenCart you can then also pass the information along.

If this is an update to their user profile, can you not create a sidebox link to the page and in the code call for the form generation and pass the session variable along.

I thinbk that we are trying to state that if you start this from within ZenCart you will have better luck. I can see no way or reason that a user would be on an outside page trying to do what you want without first being in the cart and logged in.

Zen-Venom Get Bitten

23 Nov 2006, 1:57 PM
#10
Join Date:
Mar 2005
Posts:
24
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

The shop area is only a part of the entire web site.

I have a navigation toolbar outside of ZenCart. There is a link within this toolbar that takes them to a page (outside ZenCart) where they can update their User Profile where they can register things they are interested in etc.

I don't want users to have to login twice within the web site (ie. once to update their profile and once to use the shopping cart) so I'd rather use ZenCart to enable the users to login.

I then want to use the 'customer_id' session variable as the user variable to update their profile info.

Andy

26 Nov 2006, 7:47 PM
#11
blindside avatar

blindside

Totally Zenned

Join Date:
Sep 2004
Posts:
1,237
Plugin Contributions:
1

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Look at the session code in the Zen cart headers, includes/init_includes/init_sessions.php in particular. There you can see how the Zen Cart session is built, then just pull the necessary pieces and place them at the start of your other pages. If it's all within the same site, you should be able to call the same function files, etc. without too much modification.

[email protected]:

Out of curiosity - are 'you' able to reference the value of the 'customer_id' session variable in a page outside ZenCart on your web site?
I'm not 100% sure what you mean, but it sounds like you're asking if I can get a non-Zen Cart page on my site to see the $_SESSION['customer_id'] variable. The answer I gave above applies here, so it's a "yes." Pages within a single domain can all access the same session. So if your store is at www.mysite.com/store, those Zen variables can be seen at www.mysite.com/otherpages.

However, mystore.mysite.com (note the subdomain) is not visible at www.mysite.com. That's the security limitation I was talking about before.

Frank Koehl
"Cleverly Disguised as a Responsible Adult"

frankkoehl.com

17 May 2008, 6:10 PM
#12
patchsms avatar

patchsms

New Zenner

Join Date:
May 2008
Posts:
3
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

I found this after posting in another thread about the same issue, I believe in theory you are correct about looking at init_sessions.php.
Having said that it also looks like an enormous amount of work as that file is filled with zen_* functions ad zen specific Definitions and from what I am initially seeing to do it is going to require either replicating/rewriting a LOT of Zen Code and causing a maintenance nightmare upon a zen-cart update.
it it was a simple matter of doing a require_once("init_sessions.php"); that would be fine however it's a lot more complicated than that

17 May 2008, 8:14 PM
#13
patchsms avatar

patchsms

New Zenner

Join Date:
May 2008
Posts:
3
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

OK fried my brain a little on this but I have a solution that seems to work. I'll post it after I clean it up some and maybe someone who knows what they are really doing can look at my solution and fix/make better/laugh at me or whatever.

20 May 2008, 5:16 PM
#14
robear avatar

robear

New Zenner

Join Date:
Apr 2008
Location:
Winnipeg, Canada
Posts:
12
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Would you be so kind as to show an example of the Special Link. I have a sidebox with a link to an external file (dir is beside the cart) and within the same domain. I have been unsuccessful accessing variables from within the external dir.

rene

BlindSide:

You could pass it via a form using POST or GET, just insert the value of $_SESSION['customers_id']. I do this on two sites where I link users' login session. I use a special link, pass the necessary variables, and perform some verification on the other side.

24 Jun 2008, 7:48 PM
#15
dudhi avatar

dudhi

New Zenner

Join Date:
May 2008
Posts:
10
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

If you found the solution, can you please share. I have the same problem, page is within a domain and can't access session.

26 Jun 2008, 9:55 PM
#16
swfu avatar

swfu

New Zenner

Join Date:
Jun 2008
Posts:
6
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

I too need to find a solution to this problem also. I have zen cart installed in https://www.mysite.com/cart and need to access the session variables from the root of the webserver. I need to do this so that I can display a link to login or logout, shopping cart, my account, checkout from other php pages.

Anyone else trying to do the same?

3 Jun 2009, 5:57 AM
#17
developer_gtpl avatar

developer_gtpl

New Zenner

Join Date:
May 2009
Posts:
16
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Hi,
I want to use the value of the $_SESSION['customer_id'] variable in pages outside of ZenCart in the same domain.
Pls help me on that.
Thanks.

22 Jun 2009, 7:04 PM
#18
svhdesigns avatar

svhdesigns

New Zenner

Join Date:
May 2009
Posts:
1
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

There seems to be a growing interest in this customer session solution. I am attempting the same end result, but have been unsuccessful to this point with my testing. My goal is to keep the zen cart store separate from the rest of the website pages, but would like to maintain the customer log in session throughout all the pages using a log in side box and also include a site wide shopping cart side box.

Tonight I will be testing another possiblity using 'chdir'. I am hoping that the chdir method might help in this matter, but I am not sure how it will react with the Zen Cart sessions, as the typical php session handling rules have not applied thus far.

If anyone else has been successful with this please feel free to post and maybe we can all get some sleep instead of endless R&D testing to find a solution.

S

24 Feb 2011, 3:57 AM
#19
stephenacre avatar

stephenacre

New Zenner

Join Date:
Feb 2011
Posts:
8
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

Did you ever post your solution? I am struggling with this and would be interested in how you resolved it.

25 Feb 2011, 2:17 AM
#20
pstroot avatar

pstroot

New Zenner

Join Date:
Feb 2011
Posts:
1
Plugin Contributions:
0

Re: referencing $_SESSION['customer_id'] variable outside of ZenCart

I've been working with this same issue and thought I would post my solution to the problem. I also could not access any information about the users session or the shopping cart outside of my zencart store directory, so what I did was opened up an invisible page inside an iFrame, and used JavaScript to send the information I needed to the parent page. It looked something like this.

This is the script on the page I created that I placed inside my zencart store directory. I named this "customerInformation.php". It finds out whether the user is logged in or not, what their cart total is, number of items, cart ID, security token and customer first name:

<script>
  <?php if ($_SESSION['customer_id']) { ?>
    parent.setValue("isLoggedIn","true");
  <?php } else { ?>
    parent.setValue("isLoggedIn","false");
  <?php } ?>
  parent.setValue("total",<?=$_SESSION["cart"]->total;?>);
  parent.setValue("totalItems",<?=$_SESSION["cart"]->count_contents();?>);
  parent.setValue("cartID","<?=$_SESSION["cart"]->cartID;?>");
  parent.setValue("securityToken","<?=$_SESSION["securityToken"];?>");
  parent.setValue("customer_first_name","<?=$_SESSION["customer_first_name"];?>");
</script>

Then, On the page of my website where I need to access that information, I opened customerInformation.php in an iFrame with a zero width and height so it cannot be seen, and I added this javascript code:

function setValue(name,value){
	if(name == "isLoggedIn" ){		
		if(value == "true"){
		    // show logout button
		} else {
		    // show login button
		}
	} else if(name == "total"){
		// display the cart total		
	} else if(name == "totalItems"){
		//display total items in cart
	} else if(name == "securityToken"){
		// do what you with with this
	} else if(name == "customer_first_name"){
	        // display the users name
	}
		
}

The JavaScript functions would either show or hide relevant div tags, or populate text into an area using "innerHTML". This unfortunately won't help you if you need to process the information with PHP on the server side, but if it's simply about displaying information to the user, it may be just what you need.