473,395 Members | 1,742 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,395 software developers and data experts.

Issue with the casting of a SESSION variable

Good day,

I have the a bit of an issue with retrieving an object from php's
session.

I set a session variable "user" from the class User as follows:
$user = new User();
// ... do some stuff to $user
$_SESSION['user'] = serialize($user);

And try to retrieve the variable again later:
$user=(User)unserialize($_SESSION['user']);

But the last statement throws this error:
Parse error: syntax error, unexpected T_STRING in ...

Google did not produce any useful solutions to this problem; Other
than syntax issues, but I'm 100% sure it is not an earlier syntax
issue. The problem lies in the casting of session object.

Any help would be appreciated.

Regards,
Andre
Oct 29 '08 #1
4 4378
On Oct 29, 4:00*pm, AndreH <aha...@gmail.comwrote:
Good day,

I have the a bit of an issue with retrieving an object from php's
session.

I set a session variable "user" from the class User as follows:
$user = new User();
// ... do some stuff to $user
$_SESSION['user'] = serialize($user);

And try to retrieve the variable again later:
$user=(User)unserialize($_SESSION['user']);

But the last statement throws this error:
Parse error: *syntax error, unexpected T_STRING in ...

Google did not produce any useful solutions to this problem; Other
than syntax issues, but I'm 100% sure it is not an earlier syntax
issue. The problem lies in the casting of session object.

Any help would be appreciated.

Regards,
Andre
Hello Andre.

You are trying (type-casting) while retrieving the details from
session to the variable.
There is no such type "User".
and also no need to add that.
Try without "(User)".
Oct 29 '08 #2
On Oct 29, 1:08*pm, Suhas Dhoke <suhasdh...@gmail.comwrote:
On Oct 29, 4:00*pm, AndreH <aha...@gmail.comwrote:
Good day,
I have the a bit of an issue with retrieving an object from php's
session.
I set a session variable "user" from the class User as follows:
$user = new User();
// ... do some stuff to $user
$_SESSION['user'] = serialize($user);
And try to retrieve the variable again later:
$user=(User)unserialize($_SESSION['user']);
But the last statement throws this error:
Parse error: *syntax error, unexpected T_STRING in ...
Google did not produce any useful solutions to this problem; Other
than syntax issues, but I'm 100% sure it is not an earlier syntax
issue. The problem lies in the casting of session object.
Any help would be appreciated.
Regards,
Andre

Hello Andre.

You are trying (type-casting) while retrieving the details from
session to the variable.
There is no such type "User".
and also no need to add that.
Try without "(User)".
Thanks for the reply.

I removed the "(User)" part as follows:
$user=unserialize($_SESSION['user']);

But get an exception when I call one of its functions:

if($user->getSuperUser()) // Do something...

Here is the exception:
Fatal error: Call to a member function getSuperUser()

It works if I go:
$user = new User();
if($user->getSuperUser()) // Do something...

Here's my User class:
class User
{
var $ID = 0;
/* Some more members ... */
var $SuperUser = false;

function setValues($ID, /* Some more members ... */
$SuperUser)
{
$this->ID = $ID;
/* Some more members ... */
$this->SuperUser = $SuperUser;
}

function getSuperUser()
{
return $this->SuperUser;
}
}
Oct 29 '08 #3
On Oct 29, 1:37*pm, AndreH <aha...@gmail.comwrote:
On Oct 29, 1:08*pm, Suhas Dhoke <suhasdh...@gmail.comwrote:
On Oct 29, 4:00*pm, AndreH <aha...@gmail.comwrote:
Good day,
I have the a bit of an issue with retrieving an object from php's
session.
I set a session variable "user" from the class User as follows:
$user = new User();
// ... do some stuff to $user
$_SESSION['user'] = serialize($user);
And try to retrieve the variable again later:
$user=(User)unserialize($_SESSION['user']);
But the last statement throws this error:
Parse error: *syntax error, unexpected T_STRING in ...
Google did not produce any useful solutions to this problem; Other
than syntax issues, but I'm 100% sure it is not an earlier syntax
issue. The problem lies in the casting of session object.
Any help would be appreciated.
Regards,
Andre
Hello Andre.
You are trying (type-casting) while retrieving the details from
session to the variable.
There is no such type "User".
and also no need to add that.
Try without "(User)".

Thanks for the reply.

I removed the "(User)" part as follows:
$user=unserialize($_SESSION['user']);

But get an exception when I call one of its functions:

if($user->getSuperUser()) // Do something...

Here is the exception:
Fatal error: Call to a member function getSuperUser()

It works if I go:
$user = new User();
if($user->getSuperUser()) // Do something...

Here's my User class:
* * class User
* * {
* * * * var $ID = 0;
* * * * /* Some more members ... */
* * * * var $SuperUser = false;

* * * * function setValues($ID, /* Some more members ... */
$SuperUser)
* * * * {
* * * * * * * * $this->ID = $ID;
* * * * * * * * /* Some more members ... */
* * * * * * * * $this->SuperUser = $SuperUser;
* * * * }

* * * * function getSuperUser()
* * * * {
* * * * * * * * return $this->SuperUser;
* * * * }
* * }
Ok found my problem.

Somehow the $_SESSION["user"] variable is set to the boolean value
"false", even though this assignment is never made. So my program
tries to access the SuperUser method, which obviously doesn't exist
for a boolean.

Thanks for the help Suhas.
Oct 29 '08 #4
AndreH wrote:
Good day,

I have the a bit of an issue with retrieving an object from php's
session.

I set a session variable "user" from the class User as follows:
$user = new User();
// ... do some stuff to $user
$_SESSION['user'] = serialize($user);

And try to retrieve the variable again later:
$user=(User)unserialize($_SESSION['user']);
Why? A session already serializes its values when stored, no need to do
it twice. You can perfectly store an object in a session, as long as
you've loaded the class definitions before session_start().

Keep in mind not every object can be serialized, see the manual:
http://nl2.php.net/serialize
"serialize() handles all types, except the resource-type. You can even
serialize() arrays that contain references to itself. Circular
references inside the array/object you are serialize()ing will also be
stored. Any other reference will be lost."

Something not serializable is by default not storable in a session,
allthough you could employ the __sleep() & __wakeup() methods to remove
any unserializable content, and recreate them on unserialize.
--
Rik
Oct 29 '08 #5

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

Similar topics

3
by: JP SIngh | last post by:
Hi All We have an ASP application which tracks holidays for our employees. When the user logs in we store thier username in a session variable and use that variable when displaying and adding...
1
by: Rajesh Abraham | last post by:
I am new to Asp world and I have a doubt as follows. I have a class called objTranslator, which I declare and initiate global.asax as follows. Session_Start csTranslator.objTranslator...
0
by: Rajesh Abraham | last post by:
I am new to Asp world and I have a doubt as follows. I have a class called objTranslator, which I declare and initiate global.asax as follows. Session_Start csTranslator.objTranslator...
6
by: Carlo Marchesoni | last post by:
I have an ASP.NET/C# solution, where I can perfectly cast something I stored in the session object to a class of mine (BackEnd), as this: ->be = (BackEnd)Session;<- But if I try to do the same:...
6
by: Mike | last post by:
I have a function that is called when the user clicks the submit button, during this function i also set a varaible to "Y" due to that this function does a post back to the page then redirects....
5
by: Diffident | last post by:
Hello All, I have a 2-dimensional array that I am storing as a session variable. I have no idea on how I can cast the session variable back to 2-dimensional array. Any pointers? Reference...
5
by: paladin.rithe | last post by:
I'm running into an issue with session_start(). I see that you can't run it twice, otherwise it causes an issue. That's fine, and makes sense. I also saw some ideas on how to get around this if...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
2
by: =?Utf-8?B?R2Vvc3Ns?= | last post by:
Dear All, I try to change a master page in the OnPreInit in a asp.net page. (Session variable is guaranteed to have been assigned) protected override void OnPreInit(EventArgs e){ if...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.