News:

Wondering if this will always be free?  See why free is better.

Main Menu

SMF and session

Started by MarcoK, April 21, 2010, 04:41:23 AM

Previous topic - Next topic

MarcoK

Hi at all.

I'm doing to write a script for including wfCart() a shopping cart session based into SMF.

I have a trouble saving data into session.

I use this class as example

class Cart {
var $nuovo = 0;
var $vecchio = 0;
function cart(){} //costruttore di classe;

function add($v){
   $this->vecchio=$this->nuovo;
   $this->nuovo=$v;
}
function get(){
  $obj['vecchio']=$this->vecchio;
  $obj['nuovo']=$this->nuovo;
  return $obj;
}
}


I call the class and assign it at the session

$cart =new Cart();
$_SESSION['cart']=$cart;


And i print the $_SESSION:
Array (
[session_value] => 0f565ed469f2d82f9d246227fcce4164
[cart] => Cart Object (
  [nuovo] => 0
  [vecchio] => 0
  )
)

The session was create.

When i send a value for VAR nuovo, i obtain

Array (
[session_value] => 0f565ed469f2d82f9d246227fcce4164
[cart] => Cart Object (
  [nuovo] => 1
  [vecchio] => 0
  )
)

so, the class will work and olso the session.

But if now i send a new value for VAR nuovo, i must obtain [nuovo] => 2 [vecchio]=> 1.

But is not what i obtain:
Array (
[session_value] => 0f565ed469f2d82f9d246227fcce4164
[cart] => Cart Object (
  [nuovo] => 2
  [vecchio] => 0
  )
)


WHY?

Maybe the SMF clea eachtime the session??

Arantor

How exactly are you trying to integrate them? SMF does its own session management too.

MarcoK

Yes i have see.

In fact, i must use the session of smf, by create a new cell: $_SESSION['cart'].


My intention is only use a session to save data from fwcart. And i have made a plugin that open a new page and show the items to sell.

How i can exam the SMF session managment?

Thanks for your answer

MarcoK

I have found into Sources/load.php
these function:

sessionWrite and SessionRead.

I think i must use those function to manage the session into SMF. But there is a tutorial to learn how i must proceed?

Arantor

First up, are you loading SSI.php at all? This will kick in SMF's session functions. Then you just use $_SESSION, no need to session_start() or whatever.

MarcoK

#5
Hum... i'have try to require SSI.php into my file but probably it will alrady loaded becose php alert my that the function ssi_shutdown() was alrady declared.

I'm not working on external file but on an mod of SMF.

I must use a session into a this mod.

Arantor

If you're using a mod, you should just be able to call $_SESSION directly. No extra work, no session_start, just use $_SESSION.

MarcoK

There is something wrong!

If I use only $_SESSION it will not function!
Maybe i do a mistake into a class call?

Arantor

Please define "will not function"

SMF starts its own sessions, $_SESSION is a superglobal variable meaning it is always available.

MarcoK

Ok.

The code in the first post is into a shop.php in source directory, called by a menu: index.php?action=shop.

The complate function on shop.php is:
function shop(){
global $obj;

$cart=& $_SESSION['cart'];
$cart =new Cart();

if($_POST['val']){
$cart->add($_POST['val']);
}

$obj=$cart->get();
}



In the shop.template.php

function template_main(){
global $obj;

echo 'Nuovo:'.$obj['nuovo'].' --- Vecchio:'.$obj['vecchio'];

echo'<a href="?action=shop&amp;val=1">Valore 1</a><a href="?action=shop&amp;val=2">Valore 2</a>';
}


When i click on Val1, the script (i think) save into $_SESSION by the class Cart()  $_SESSION['Nuovo']=1 and $_SESSION['Vecchio']=0;

It's right, and function as well, in fact, when i print_r($_SESSION):
Array (
[session_value] => 0f565ed469f2d82f9d246227fcce4164
... other value...
[cart] => Cart Object (
  [nuovo] => 1
  [vecchio] => 0
  )


Now, when i click on Val2, I want to get $_SESSION['Nuovo']=2 and $_SESSION['Vecchio']=1.

but it is not!!!

The session reset my value and I get on print_r($_SESSION):

Array (
[session_value] => 0f565ed469f2d82f9d246227fcce4164
... other value...
[cart] => Cart Object (
  [nuovo] => 2
  [vecchio] => 0
  )


Why? If I create a file width this code, che Session and the class work right!!!! Why not into SMF?

Arantor

Try turning off database driven sessions in the admin panel.

MarcoK

Yesterday evening i have do same test.

The $_SESSION work correctly if i not use the class.

If i assigne the value normally such as:
if(isset($_SESSION['cart'])){
$cart=$_SESSION['cart'];
}
if($_GET['val']){
$cart['vecchio']=$cart['nuovo'];
$cart['nuovo']=$_GET['val'];
}
$_SESSION['cart']=$cart


the value are memorized.
But if i assign at the session the new Cart(), where Cart() was a class, it will reset any value.

Now i think the touble is the vars declaration into a class.

But in the orignal wfCart class, the declaration of vars in this method work correclty.

I try to turning off database driven sessions. I hope....

Arantor

Yeah, serialize'ing objects isn't very clever, generally.

MarcoK

Ok, aslo turning of the database session have no good result.

Any suggestion for resolving?

Arantor

Not storing an object in the session would solve everything, because what's happening is when the object is unserialized, it's not holding the exact object, it's instancing a new one.

Advertisement: