|
Posted by Jerry Stuckle on 07/19/07 17:02
kvnsmnsn@hotmail.com wrote:
> I've got a piece of PHP code that gets executed twice. The first time
> through, variable <$User> is unregistered, so message "jqz cbig90"
> gets printed to the screen as an error message. When I comment out
> that first <system_error( $message);>, then the first time through
> happens without a problem, and the second time through message
> "jqz cbig93" gets printed to the screen as an error message.
>
> At the same time I'm using variable <$hits> to keep track of how many
> times this code has been executed, using the format specified on page
> 183 of the book _Programming PHP_ by Lerdorf and Tatroe. So the first
> time through <$hits> should have value 1, and after I commented out
> the first <system_error( $message);> it should have value 2. But in
> this latter case the value displayed on the screen is still 1.
>
> Does anybody know why this is happening? How can I write my code so
> that the first time through this section <$hits> is 1 but the second
> time through this section <$hits> is 2? Any information on this would
> be greatly appreciated.
>
> ---Kevin Simonson
>
> "You'll never get to heaven, or even to LA,
> if you don't believe there's a way."
> from _Why Not_
>
> ####################################################################
>
> session_start();
>
> global $hits;
> session_register( 'hits');
> ++$hits;
>
> ..........<snip>..........
>
> if( !session_is_registered( "User" ) ) {
> session_register( "User" );
> $User = new User();
> $message = sprintf( "jqz cbig90--\$hits == %d.", $hits);
> system_error( $message);
> } else if( !is_object( $User ) ) {
> $message = sprintf( "jqz cbig93--\$hits == %d.", $hits);
> system_error( $message);
> system_error( "Unable to instan_tiate User object." );
> } // end if
>
> ####################################################################
>
> Function <system_error> is just:
>
> ####################################################################
>
> function system_error( $error ) {
> die
> ( "<div style=\"font: normal 12px verdana;\"><b>System Error:</b>
> "
> . $error . "</div>" );
> }
>
1. You never initialized $hits.
2. Don't use session_register. Use $_SESSION['hits'] instead.
if (!isset($_SESSION['hits'])
$_SESSION['hits'] = 1;
else
$_SESSION['hits']++;
Or
$_SESSION['hits']=isset($_SESSION['hits']) ? $_SESSION['hits'] + 1 : 1;
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|