I'll start with the code snippet... Then I'll tell you what doesn't work... Here it is:
/
First page...
Session var created, user-object initialzed, user-data created in db and stored in session-var
/
session_start();
include("inc/inc_classes.php");
session_register("objUser");
$objUser = New User;
$blnResult = $objUser->CreateUser($strLastName, $strFirstName, $strEmail, $strUsername, $strPassword);
...
Redirect to next page
/
Second page...
Read out data stored in session-var
/
session_start();
include("inc/inc_classes.php");
echo session_id();
...
Last name: <?=$_SESSION["objUser"]->intUserID?><br>
First name: <?=$objUser->strFirstName?><br>
/
Class: User
/
class User {
// Define variables
var $intUserID;
var $strLastName;
var $strFirstName;
var $strEmail;
var $strUsername;
var $strPassword;
// Define functions
function CreateUser($strLastName, $strFirstName, $strEmail, $strUsername, $strPassword) {
$conCU = New DBConn();
$conCU->MakeConn();
$strSQL = "INSERT INTO tblUser(strLastName, strFirstName, strEmail, strUsername, strPassword) VALUES ('".mysql_escape_string($strLastName)."', '".mysql_escape_string($strFirstName)."', '".mysql_escape_string($strEmail)."', '".mysql_escape_string($strUsername)."', '".mysql_escape_string($strPassword)."')";
$blnResult = mysql_query($strSQL) or die(mysql_error());
$intInsertID = mysql_insert_id();
if (blnResult == FALSE) {
return $blnResult;
} elseif ($intInsertID == 0) {
return FALSE;
} else {
$this->intUserID = $intInsertID;
$this->strLastName = $strLastName;
$this->strFirstName = $strFirstName;
$this->strEmail = $strEmail;
$this->strUsername = $strUsername;
$this->strPassword = $strPassword;
return TRUE;
}
}
Oki... The session is registered (I find the sess###-file in my temp-dir), the values (LastName, FirstName,...) are stored in the database and in the session (Because when I open the sess###-file I can see the values)...
Everything seems to work... BUT NOT!!!
On the next page, I can display the session_id, but I can't display the values in the session. When I try different methods, like $_SESSION["objUser"]->intUserID or objUser->intUserID, it makes no difference...
Do I something wrong... (BTW: I've tried to declare the var $objUser first (by assigning a value or setting it global) and then registering it as session_var... MAKES NO DIFFERENCE).
Maybe someone who have some experience with objects in session_vars?
(I'm a ASP webdeveloper, with some years of experience... I've used PHP years ago and wanted to pick it up again, but after two days I'm allready stuck 🙁 )
And... I've checked several books, but they never use objects in session_vars the way I want it... (Mastering PHP 4.1 [Sybex], PHP and MySQL Web Development [Sams], MySQL/PHP Database Applications [M&T Books], Web Application Development With PHP 4.0 [New Riders])
Thanks in advance... I'll attach the source-code of the different pages with this message. The code is just a try-out of a little app I wanted to write to refresh my PHP-memory... It's not always the way iut has to be done, but I think it will read smoothly 🙂
Thanks again! Greetings,
Niels