Solved

Passing objects as session variables

Posted on 2008-06-19
3
1,193 Views
Last Modified: 2010-05-18
It's my understanding that I can pass objects around during a session by simply assigning them to a session variable, as long as the class is defined before I run session_start().  That said, I'm having problems finding the right solution.  I'm trying it the way I've described above, as well as serializing/unserializing and then passing into/out of session variables.  What might I be doing wrong?
<?php
 

// include Timecard class definition

require("Timecard.php");
 

session_start();
 

if($_SESSION['logged_in_userID']) {

	$tc = new Timecard($_SESSION['logged_in_userID'],"");

        // setup page header -- removed for clarity

	// display timecard -- removed for clarity

	$_SESSION['currentTimecard'] = $tc;

} else {

	// create a new timecard -- removed for clarity

}
 

?>

Open in new window

0
Comment
Question by:TechComposer
  • 2
3 Comments
 
LVL 2

Author Comment

by:TechComposer
ID: 21825453
To follow is the script in which I'm attempting to reload this object.  When calling "print_r($_SESSION)", I am given:

Array ( [logged_in_userID] => 1 [logged_in_admin] => 1 [currentTimecard] => Timecard Object ( [userID:private] => 1 [date:private] => 2008-06-19 11:21:53 [timecardID:private] => 252 [timecardClosed:private] => 0 [timecardActivities] => Array ( ) ) )
<?php
 

require("Timecard.php");
 

session_start();
 

if($_SESSION['logged_in_userID']) {

	// setup page header -- removed for clarity

	

	$tc = $_SESSION["currentTimecard"];

	echo "User ID: " . $tc->getUserID . "<br>";

	echo "<h3>Session Variables:</h3>";

	print_r($_SESSION);

}
 

?>

Open in new window

0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 21825480
I think you are missing two anchors:

Have you tried using  $tc->getUserID() instead of $tc->getUserId ?

Kind regards

-r-
0
 
LVL 2

Author Comment

by:TechComposer
ID: 21825642
Well, don't I look like a moron...
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now