For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > September 2004 > Passing Objects Via Session in PHP 4









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Passing Objects Via Session in PHP 4
Joseph Crawford

2004-09-27, 3:55 am

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
Joseph Crawford

2004-09-27, 3:55 am

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com