How to Store a Class Instance in a $_Session Space

Can I store a class instance in a $_SESSION space?

You must first serialize the object in to a string:

$_SESSION['user'] = serialize($user);

and:

$user = unserialize($_SESSION['user']);

Just make sure that the class is first defined before unserializing the object.

PHP: Storing 'objects' inside the $_SESSION

I know this topic is old, but this issue keeps coming up and has not been addressed to my satisfaction:

Whether you save objects in $_SESSION, or reconstruct them whole cloth based on data stashed in hidden form fields, or re-query them from the DB each time, you are using state. HTTP is stateless (more or less; but see GET vs. PUT) but almost everything anybody cares to do with a web app requires state to be maintained somewhere. Acting as if pushing the state into nooks and crannies amounts to some kind of theoretical win is just wrong. State is state. If you use state, you lose the various technical advantages gained by being stateless. This is not something to lose sleep over unless you know in advance that you ought to be losing sleep over it.

I am especially flummoxed by the blessing received by the "double whammy" arguments put forth by Hank Gay. Is the OP building a distributed and load-balanced e-commerce system? My guess is no; and I will further posit that serializing his $User class, or whatever, will not cripple his server beyond repair. My advice: use techniques that are sensible to your application. Objects in $_SESSION are fine, subject to common sense precautions. If your app suddenly turns into something rivaling Amazon in traffic served, you will need to re-adapt. That's life.

how to save many objects in a $session array variable

This is working for me locally.

Define your items session variable as an array, then push them into the variable using array_push

class product {

public $prodId;
public $prodName;
public $prodPrice;

public function __construct($prodId, $prodName, $prodPrice) {
$this->prodId = $prodId;
$this->prodName = $prodName;
$this->prodPrice = $prodPrice;
}

public function get_prodId() {
return $this->prodId;
}

public function get_prodName() {
return $this->prodName;
}

public function get_prodPrice() {
return $this->prodPrice;
}
}

Then use it like so:

$product = new product(1, "test", 23);
$product2 = new product(2, "test2", 43);

$_SESSION['items'] = array();
array_push($_SESSION['items'], $product, $product2);
echo '<pre>';
print_r($_SESSION['items']);
echo '</pre>';

This is the output of print_r()

Array
(
[0] => product Object
(
[prodId] => 1
[prodName] => test
[prodPrice] => 23
)
[1] => product Object
(
[prodId] => 2
[prodName] => test2
[prodPrice] => 43
)

)

PHP Session with an Incomplete Object

It's missing the serialize/unserialize of your template class.

Take a look here for an working example I gave on another question of yours.

For instance, you probably want this:

<?php
$_SESSION['template'] = serialize($template);
?>

and

<?php
$template = unserialize($_SESSION['template']);
?>

Edit:

reading your comment about moving it to the top gives one hint.

The automatic serialization/unserialization occurs when you call session_start().

That means the order in which you include your files and call the session_start() is very important.

For example:

This would be wrong:

<?php
session_start();
include 'inc/template.class.php';
?>

While this would be correct:

<?php
include 'inc/template.class.php';
session_start();
?>

Now, I see in your example that it is in the CORRECT order, but I also notice you do many other includes before including template.class.php

Would it be possible that one of those includes (perhaps prep.php or header.class.php) does call start_session() too?

If yes, that was your issue (session_start() being called before your template.class.php).

Using PHP SESSION Variables to store MySQL query results

Store the actual array in session:

File1.php:

$query = //my query.
$result = array();
$res = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($res, MYSQL_NUM){
$result[] = $row;
}
$_SESSION['query2'] = $result;

File2.php:
//I have already done session_start(); and initialized $conn

$tempArray = $_SESSION['query2'];
var_dump($tempArray);


Related Topics



Leave a reply



Submit