473,408 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

-forgot- Storing objects in a session PHP 5.2.0

I have read the message boards in this group and others. I still have
not been able to pull my obect out of a session and use it. Here is how
I store it in a session:

<?
//Include the UserClass & RoleClass so that we can create a user
object.
require_once("UserClass.php");
require_once("RoleClass.php");

//This has to be the first line of code a file if sessions are used.
session_start();

//Create a new user object
$user = new User();
$user->setName($_SERVER['HTTP_OBLIX_GIVENNAME']);

$role = new Role();
$role->setSysName($xml->roles[$i]->systemName);
//Now add the role to the user object.
$user->addRole($role);

//Save the user object in the session
$_SESSION['user'] = $user;
?>

This is how I try to pull the object out:

<?
require_once("UserClass.php");
require_once("RoleClass.php");

session_start();

//Include the UserClass & RoleClass so that we can create a user
object.
//See if there is a user object in the session
if ( isset($_SESSION['user']) ){

//Get user object from session
$user = (User)$_SESSION['user'];
}

I have also just tried:
$user = $_SESSION['user'];

?>

I receive errors like these:
<b>Parse error</b>: syntax error, unexpected T_VARIABLE in
<b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
<b>14</b><br />

I also get this message:
<b>Warning</b>: session_start() [<a
href='function.session-start'>function.session-start</a>]: Node no
longer exists in
<b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
<b>5</b><br />

Nov 27 '06 #1
2 23781
Hi Anthony --

This may turn out to be a dead end, but I have to ask anyway. :) Is
auto_start enabled for sessions in your php.ini file? If it is, this
might be putting the kibosh on your attempts to save an object to a
session variable.

Geoffrey
Anthony Smith wrote:
I have read the message boards in this group and others. I still have
not been able to pull my obect out of a session and use it. Here is how
I store it in a session:

<?
//Include the UserClass & RoleClass so that we can create a user
object.
require_once("UserClass.php");
require_once("RoleClass.php");

//This has to be the first line of code a file if sessions are used.
session_start();

//Create a new user object
$user = new User();
$user->setName($_SERVER['HTTP_OBLIX_GIVENNAME']);

$role = new Role();
$role->setSysName($xml->roles[$i]->systemName);
//Now add the role to the user object.
$user->addRole($role);

//Save the user object in the session
$_SESSION['user'] = $user;
?>

This is how I try to pull the object out:

<?
require_once("UserClass.php");
require_once("RoleClass.php");

session_start();

//Include the UserClass & RoleClass so that we can create a user
object.
//See if there is a user object in the session
if ( isset($_SESSION['user']) ){

//Get user object from session
$user = (User)$_SESSION['user'];
}

I have also just tried:
$user = $_SESSION['user'];

?>

I receive errors like these:
<b>Parse error</b>: syntax error, unexpected T_VARIABLE in
<b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
<b>14</b><br />

I also get this message:
<b>Warning</b>: session_start() [<a
href='function.session-start'>function.session-start</a>]: Node no
longer exists in
<b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
<b>5</b><br />
Nov 28 '06 #2
"Anthony Smith" <mr******@hotmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>I have read the message boards in this group and others. I still have
not been able to pull my obect out of a session and use it. Here is how
I store it in a session:

<?
//Include the UserClass & RoleClass so that we can create a user
object.
require_once("UserClass.php");
require_once("RoleClass.php");

//This has to be the first line of code a file if sessions are used.
session_start();

//Create a new user object
$user = new User();
$user->setName($_SERVER['HTTP_OBLIX_GIVENNAME']);

$role = new Role();
$role->setSysName($xml->roles[$i]->systemName);
//Now add the role to the user object.
$user->addRole($role);

//Save the user object in the session
$_SESSION['user'] = $user;
Not like this...
?>

This is how I try to pull the object out:
Wrong again. You can't store objects into a session per se, you need to
serialize and unserialize them in order to use them in a session. Here's
something you should read:
http://php.net/manual/en/language.oop.serialization.php

When you store the object to the session, first serialize it. Then, when
retrieving it, you unserialize it.

page 1:
$_SESSION['user'] = serialize($user);

page 2:
$user = unserialize($_SESSION['user']);

And remember to always include the class definition on all pages where you
use the class.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
12
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
4
by: Frank Rizzo | last post by:
In classic ASP, it was considered a bad idea to store VB6-created objects in the Application variable for various threading issues. What's the current wisdom on storing objects in the Application...
3
by: MrShovel | last post by:
I'm new to this ASP.NET caper and have the following questions. I have a TestObject that contains about 50 fields of data and 3 member procedures. Below is a simplified explanation of what I do....
10
by: Mark Rae | last post by:
Hi, This relates to the previous thread "Disappearing Sessions", but is a bit more generic so I thought I'd start a new thread. This one relates to the storing of objects in Session once only to...
9
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would...
1
by: Miesha.James | last post by:
Hello, I'm trying to rewrite visual c++ code into visual c++ .NET code and I've run across a problem with storing objects into a list. Here;s an example of the code I have: ref struct...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.