Zen Cart Logo
If this is your first visit, check our documentation. To post, register or login.
Forums / General Questions / General Questions / My idea for accessing zen cart sessions outside of zen...

My idea for accessing zen cart sessions outside of zen...

Locked

Started by glenelkins on 5 Sep 2008, 11:09 AM · Views: 5,593

Results 1 to 20 of 26
This thread is locked. New replies are disabled.
5 Sep 2008, 11:09 AM
#1
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #1 by glenelkins

Hi

I know allot of people have been asking how to mainly access $_SESSION['customer_id'] from outside zen cart.

I also have posted a few times asking until it dawned on me, perhaps i could use SOAP to get the session data i need. I finally got the system working and would like to share my code with you guys and perhaps get some feedback??

Ok first off we need to download "NuSOAP" and extract the files into the following folder structure:

ZEN_CART_ROOT/nusoap/lib/FILES_HERE

Create the following file:

ZEN_CAR_ROOT/session_get.php

Within session_get.php the following code:

<?php
// Include application top
include_once ( "includes/application_top.php" );


function get_session( $var ){

	return ( settype ( $_SESSION['customer_id'], "string" ) );
}

// Load up nusoap library
require_once('nusoap/lib/nusoap.php');

// Create soap object
$Soap_Obj = new soap_server();

// COnfigure WSDL
$Soap_Obj->configureWSDL ( "session_get", "urn:session_get" );

// Register callback function
$Soap_Obj->register('get_session',
		array ( "var" => 'xsd:string'),
		array ( "return" => 'xsd:string'),
		'urn:session_get',
		'urn:session_get#get_session',
		'rpc',
		'encoded',
		'Returns Zen Cart Session Var'
	
		);

// Wait for response
$Soap_Obj->service($HTTP_RAW_POST_DATA);


?>

Now from the script you want to get the data to , for example the "customer_id" you use the following code:

<?php
// Load up nu_soap
require_once('ZEN_CART_ROOT/nusoap/lib/nusoap.php');


//now we must create a soapclient object
$soapclient = new nusoap_client('http://YOURDOMAIN.COM/ZEN_CART_ROOT/session_get.php?wsdl',true);

// Call get session variable function from server
$result = $soapclient->call ( "get_session", array ( "var" => "customer_id" ) );


if ( $client->fault ) {

	echo "<h2>Fault</h2><pre>";
	print_r ( $result );
	echo "</pre>";

} else {
	// trap error
	$err = $soapclient->getError();

	if ( $err ) {

		echo "<h2>Error</h2><pre>" . $err . "</pre>";
		print_r ( $result );
		echo "</pre>";

	} else {

		echo "<h2>Result</h2><pre>";
		echo $result;
		echo "</pre>";
		
	}

}	
?>

This piece of code is where we choose what variables to return:

// Call get session variable function from server
$result = $soapclient->call ( "get_session", array ( "var" => "customer_id" ) );

Hope this helps people!

5 Sep 2008, 11:28 AM
#2
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #2 by glenelkins

This has a small bug im trying to fix right now

5 Sep 2008, 1:29 PM
#3
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #3 by glenelkins

Iv just realised that with SOAP you cannot handle SESSIONS without actually changing a bunch of code on zen carts login and session handling code.

The best way iv found is write a small php script like this

<?php

// Include application top
include_once ( "includes/application_top.php" );

// Get the var we want
$var = $_GET['var'];

echo $_SESSION[$var];

?>

Place it in the zen cart root folder.

Then send over the variable you want using AJAX like this:

URL = ZEN_CART_ROOT/script_to_get_session.php?var=customer_id

This will then return the current logged in customer id to your javascript

5 Sep 2008, 7:53 PM
#4
drbyte avatar

drbyte

Sensei

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

Post #4 by drbyte

I hope you're planning to do a complete security audit of your custom code at some point, to close all the holes that creates ...

.
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 Sep 2008, 8:43 AM
#5
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #5 by glenelkins

hi

can you point out security holes that you notice already and ill fix them up

6 Sep 2008, 8:53 AM
#6
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #6 by glenelkins

in fact i think i may have an idea to use the SOAP client and server with a new database table and the zen cart $_COOKIE['zenid']

If I modify the login of zen cart i#to insert info into a custom db table such as session_id, session and timeout i can actually pass the zen sesison id to the soap server and check the database for a) that session id exists b) that session hasnt timed out. If the timeout is set the same as the zen cart one it should work ok

also edit the logout routine to delete the session info from the custom table.

any ideas on this?

6 Sep 2008, 12:21 PM
#7
a_hot_mess avatar

a_hot_mess

New Zenner

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

Post #7 by a_hot_mess

glenelkins...

Please don't take this as being antagonistic (I know people over here LOVE to threadcrap just so they can get a dig at someone thinking outside the box), but what will you be using this information for? I'm sure it's far beyond my own needs, but I'm always interested to see what others are doing.

6 Sep 2008, 12:36 PM
#8
drbyte avatar

drbyte

Sensei

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

Post #8 by drbyte

glenelkins:

hi

can you point out security holes that you notice already and ill fix them up

My point was ... Calling something like /script_to_get_session.php?var=whatever to get the contents of ANY session var without validating who/what is making that request could easily disclose information that has no business being shared outside.

.
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.

7 Sep 2008, 7:18 AM
#9
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #9 by glenelkins

A Hot Mess... I cannot really tell you what this is used for its a confidential piece of work im doing ....at work lol. I work for a media company as a web developer and basically have been looking for a way to link Textpattern to Zen Carts login

I know allot of people have been trying to share session information outside zen cart. And iv been working to find a solution, the two main ways forward i see is AJAX or SOAP... the SOAP will be the best method if i can pass the session ID over!

DrByte...the vailidation is done on the textpattern side in this case!

7 Sep 2008, 7:21 AM
#10
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #10 by glenelkins

The main thing is i cannot understand what zen cart does to stop session working outside of itself. Iv looked over the code and appart from a load of checks it does the sessions work just like any normal program..... and nobody seems to know why it does this.

Iv even tried loading up the same session name and ID in external applications, it still wont work. Pain in the ######!

Heres a question, how does Zen Cart actually control which pages are restricted access? There may be an option to completely re-do their silly login ( i say silly, its silly from a dev point of view, its not very versitile considering its open source )

7 Sep 2008, 7:24 AM
#11
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #11 by glenelkins

sorry 1 more thing...

I know people over here LOVE to threadcrap just so they can get a dig at someone thinking outside the box

I always think outside the box! In programming, if you dont you get nowhere most of the time! Bollocks to people who put others down, i dont care i have a nicely paid job to think outside the box! lol

7 Sep 2008, 8:22 AM
#12
cjpinder avatar

cjpinder

Totally Zenned

Join Date:
Apr 2007
Location:
Herts. UK
Posts:
893
Plugin Contributions:
0

Post #12 by cjpinder

glenelkins:

The main thing is i cannot understand what zen cart does to stop session working outside of itself.
It doesn't actively stop sessions working outside itself. Did you set up the session handler correctly? Most Zen Cart installations store the session information in the database and Zen Cart uses a custom session handler for this.

Regards,
Christian.

7 Sep 2008, 1:26 PM
#13
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #13 by glenelkins

well in the login code it sets $_SESSION

but if you try accessing one of the var from a script outside the folder zen cart runs in, it does work. Its strange, every application iv ever written i can access my session vars in any folder i wish from any script file i wish

7 Sep 2008, 8:04 PM
#14
cjpinder avatar

cjpinder

Totally Zenned

Join Date:
Apr 2007
Location:
Herts. UK
Posts:
893
Plugin Contributions:
0

Post #14 by cjpinder

glenelkins:

well in the login code it sets $_SESSION

but if you try accessing one of the var from a script outside the folder zen cart runs in, it does work. Its strange, every application iv ever written i can access my session vars in any folder i wish from any script file i wish

Zen Cart uses a custom session handler to store its session data. You cannot access the session data from $_SESSION until the data has been reloaded by the session handler. You'll need to read up on session_set_save_handler() and review the code in includes/functions/sessions.php .

Regards,
Christian.

8 Sep 2008, 8:27 AM
#15
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #15 by glenelkins

The _sess_read() function has a value $key that it uses. What is this? And im not sure how this parses the $key value if you look here:

session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');

Where in this line does it parse data to the _sess_read function?

8 Sep 2008, 9:17 AM
#16
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #16 by glenelkins

iv actually gone for storing session in files it works very well by getting the session id from $_COOKIE['zenid']; then unserializing the session file and checking for customer_id

:D

8 Sep 2008, 11:03 AM
#17
glenelkins avatar

glenelkins

New Zenner

Join Date:
Apr 2008
Posts:
31
Plugin Contributions:
0

Post #17 by glenelkins

So it works like this:

Set zen cart to store sessions as files.

$sess_id = $_COOKIE['zenid'];

$session_file = "/PATH/TO/ZEN/CACHE/sess_" . $sess_id;

$session_data = @file_get_contents ( $session_file );

$data = CUSTOM_UNSERIALIZE_FUNCTION ( $session_data );

foreach ( $data as $k => $v ) {

    if ( $k == "customer_id" && !empty ( $v ) ) {

        // USER IS LOGGED IN
        $customer_id = $v;

    }

}

Seems to work pretty nicely!

1 Oct 2008, 10:56 PM
#18
oboeonetwothree avatar

oboeonetwothree

New Zenner

Join Date:
Sep 2008
Posts:
24
Plugin Contributions:
0

Post #18 by oboeonetwothree

Is there any other way to access session variables outside of zen cart (but on the same server in a directory containing it) or is this it?

2 Oct 2008, 3:19 PM
#19
oboeonetwothree avatar

oboeonetwothree

New Zenner

Join Date:
Sep 2008
Posts:
24
Plugin Contributions:
0

Post #19 by oboeonetwothree

glenelkins:

...the vailidation is done on the textpattern side in this case!

If the session variable is sent over to a grassroots page(no CMS - all php/html), do you know how this sort of identity validation be accomplished in php? I have enough of an understanding of php to read through and understand most scripts, but I'm not great at getting scripts to work - at least not quickly, so any help would be amazing. : )

2 Oct 2008, 3:30 PM
#20
kobra avatar

kobra

Black Belt

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

Post #20 by kobra

oboeonetwothree,

What is it precisely that you are attempting to do in detail

Zen-Venom Get Bitten