Closed Thread Icon

Topic awaiting preservation: php session objects (Page 1 of 1) Pages that link to <a href="" title="Pages that link to Topic awaiting preservation: php session objects (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: php session objects <span class="small">(Page 1 of 1)</span>\

Neurotic (0) Inmate
Newly admitted
posted posted 10-19-2001 10:27

hi people...
i've been playing with php, and we have a huge shopping cart system happening at the moment.
i was wondering how good the session is in php?
we have in the session an order object.
in the order object is a cart array, which is an associative array of product objects, all the products in the order.
however, the product objects don't seem to persist through the session, ie the products only exist on the page when they are added.

here is some of the code, if you are interested:

// constructor
function Order ($id=0,$status="",$cart="",$cust_firstname="None",$cust_lastname="",$cust_id="",$address="",$suburb="", $postcode="",$state="",$comment="",$card_name="",$card_num="", $card_exp="") {

$this->id = $id;
$this->cart = array();
$this->cust_firstname = $cust_firstname;
$this->cust_lastname = $cust_lastname;
$this->cust_id = $cust_id;
$this->address = $address;
$this->suburb = $suburb;
$this->postcode = $postcode;
$this->state = $state;
$this->comment = $comment;
$this->card_name = $card_name;
$this->card_num = $card_num;
$this->card_exp = $card_exp;

return true;

/* Adds a product to the shopping cart */
function add_product ($id, $qty) {
$new_product = false;
if (Update_res_qty($id,$qty)) {
global $Product;

$prod = product_search_by_id($id);

$name = $prod[$Product[name]] . " " . $prod[$Product[colour]] . " " . $prod[$Product[size]];

$new_product = new Product($id,$qty,$prod[$Product[brand]], $prod[$Product[sale_price]],$name);

$this->cart[$new_product->id] = $new_product;

return $new_product;

/* product class - This classtakes care of the
* individual products in each order

class Product {
var $id;
var $qty;
var $brand;
var $sale_price; // Price each product is sold for after discount applied.
var $name;

function Product($_id, $_qty = 1, $_brand, $_sale_price = 0, $_name = "") {
$this->id = $_id;
$this->qty = $_qty;
$this->brand = $_brand;
$this->sale_price = $_sale_price;
$this->name = $_name;
return true;

/* function show() {
$str = "<td>$this->id</td>
return $str;

function set_id ($id) {
$this->id = $id;
return true;

function set_qty ($qty) {
$this->qty = $qty;
return true;

function set_sale_price($price) {
$this->sale_price = $price;
return true;

function set_name($name) {
$this->name = $name;
return true;

function amount() {
$tmp = number_format($this->qty * $this->sale_price,2);
return $tmp;

if anyone has any ideas as to why this doesn't work, that would be muchly appreciated!

b u n c h a p i x e l s

Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-19-2001 10:58

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 10-19-2001 11:33

In PHP, you must define object before calling sessions_start() in all pages where the object is referred to. Also, definition of the object must precede the session_start() where it is referred to. The easiest way would be to put your class definition in separate file and include it in all pages where you're referring to the object.

Another way (besdies defining your object before calling session_start() function) is to serialize your object and store it in session as a string. After that, when you need it, just pick string variable up and unserialize it.

Neurotic (0) Inmate
Newly admitted
posted posted 10-19-2001 11:48

thanks for the comments, guys, it looks like everything is under... control.

b u n c h a p i x e l s

« BackwardsOnwards »

Show Forum Drop Down Menu