Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Passing Objects Via Session in PHP 4
can anyone explain why this is not working?  i have done passing
objects via session before and i remember that the object class file
had to be included before the session_start for it to work, but this
doesnt seem to be working, i mean i have it set $db->test = 'page 1';
on page 1 and provide a link to page 2, but it doesnt seem to retain
the value for $test from page to page. :(

<?php

// we will do our own error handling
error_reporting(0);

// include the const.php file that holds all the necessary constants
include_once('const.php');

include_once('class/mysql.class.php');
include_once('class/template.class.php');

session_start();

function &getObject($class) {
if (!isset($_SESSION['objects'])) {
$_SESSION['objects'] = array();
}

if  (!isset($_SESSION['_singleton'][$class])
) {
$_SESSION['objects'][$class] =& new $class;
}

return $_SESSION['objects'][$class];
}

$tpl = &getObject('template');

// user defined error handling function
// taken from the example on http://us2.php.net/errorfunc
function myErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
{
global $tpl;
// timestamp for the error entry
$dt = date("Y-m-d H:i:s (T)");

// define an assoc array of error string
// in reality the only entries we should
// consider are E_WARNING, E_NOTICE, E_USER_ERROR,
// E_USER_WARNING and E_USER_NOTICE
$errortype = array (
E_ERROR          => "Error",
E_WARNING        => "Warning",
E_PARSE          => "Parsing Error",
E_NOTICE          => "Notice",
E_CORE_ERROR      => "Core Error",
E_CORE_WARNING    => "Core Warning",
E_COMPILE_ERROR  => "Compile Error",
E_COMPILE_WARNING => "Compile Warning",
E_USER_ERROR      => "User Error",
E_USER_WARNING    => "User Warning",
E_USER_NOTICE    => "User Notice",
E_STRICT          => "Runtime Notice"
);
// set of errors for which a var trace will be saved
$user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);

$err = "<errorentry>\n";
$err .= "\t<datetime>" . $dt . "</datetime>\n";
$err .= "\t<errornum>" . $errno . "</errornum>\n";
$err .= "\t<errortype>" . $errortype[$errno] . "</errortype>\n";
$err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
$err .= "\t<scriptname>" . $filename . "</scriptname>\n";
$err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";

if (in_array($errno, $user_errors)) {
$err .= "\t<vartrace>" . wddx_serialize_value($vars, "Variables") .
"</vartrace>\n";
}
$err .= "</errorentry>\n\n";

// for testing
// echo $err;

// save to the error log, and e-mail me if there is a critical user error
error_log($err, 3, "D:/htdocs/NYPHPCode/error.log");
if ($errno == E_USER_ERROR) {
mail("jcrawford@codebowl.com", "Critical User Error", $err);
$tpl->display('error.tpl');
}
}
$old_error_handler = set_error_handler("myErrorHandler");


?>

--
Joseph Crawford Jr.
Codebowl Solutions
codebowl@gmail.com
802-558-5247

For a GMail account
contact me OFF-LIST

Report this thread to moderator Post Follow-up to this message
Old Post
Joseph Crawford
09-27-04 08:55 AM


Re: Passing Objects Via Session in PHP 4
INDEX.PHP
<?php
include_once('include/global.php');

$db = &getObject('mysql');
$db->open('localhost', 'root', '');
$db->selectDb('directory');

$tpl->display('main.tpl');

$db->test = 'this is page 1';

echo '<pre>';
print_r($_SESSION);
echo '</pre>';
$db->test = 'this is page 2';
?>

PAGE2.PHP
<?php
include_once('include/global.php');

$db = &getObject('mysql');
$db->open('localhost', 'root', '');
$db->selectDb('directory');

$tpl->display('main.tpl');

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

?>

PAGE1 PRINT_R RESULTS
[mysql] => mysql Object
(
[connection] => Resource id #7
[test] => this is page 1
)

PAGE2 PRINT_R RESULTS
[mysql] => mysql Object
(
[connection] => Resource id #7
[test] =>
)


--
Joseph Crawford Jr.
Codebowl Solutions
codebowl@gmail.com
802-558-5247

For a GMail account
contact me OFF-LIST

Report this thread to moderator Post Follow-up to this message
Old Post
Joseph Crawford
09-27-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP DB archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:36 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.