How can I have an object class store into PHP session and then get it in my next page as variable. Could you help?
Here is my class.inc.php
class shop {
var $shoeType;
var $color;
public function __construct() {
$shoeTypeService = new ShoeTypeService();
$shoe = $shoeTypeService->getAllShoes();
$this->shoeType = $shoe[20];
}
}
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.
A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.
The PHP session which is accessible via the global variable $_SESSION is stored on the server as files by default. Also the reference to it (called session_id ) is stored on client side as browser cookies. If either of this is deleted, then the session becomes invalid.
session_destroy() function: It destroys the whole session rather destroying the variables. When session_start() is called, PHP sets the session cookie in browser. We need to delete the cookies also to completely destroy the session. Example: This example is used to destroying the session.
Once you instantiate the class you can assign it to the session (assuming it's started)
$_SESSION['SomeShop'] = new Shop();
or
$Shop = new Shop();
//stuff
$_SESSION['SomeShop'] = $Shop;
Keep in mind that wherever you access that object you will need the Shop Class included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With