Reply to Re: Having Trouble with a Basic Hit Counter

Your name:

Reply:


Posted by Rik on 07/19/07 15:56

On Thu, 19 Jul 2007 17:25:16 +0200, <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.

> session_start();
>
> global $hits;
> session_register( 'hits');
> ++$hits;
>
> ..........<snip>..........
>
> if( !session_is_registered( "User" ) ) {
> session_register( "User" );
> $User =3D new User();
> $message =3D sprintf( "jqz cbig90--\$hits =3D=3D %d.", $hits);
> system_error( $message);
> } else if( !is_object( $User ) ) {
> $message =3D sprintf( "jqz cbig93--\$hits =3D=3D %d.", $hits);
> system_error( $message);
> system_error( "Unable to instan_tiate User object." );
> } // end if
>

session_register() is old... And don't use globals if you don't have to.=
=

The $_SESSION array is the way to go, and is a SuperGlobal, so it's =

reachable from anywhere in your script.

session_start();
if(!isset($_SESSION['hits']) $_SESSION['hits'] =3D 0;
$_SESSION['hits']++;

if(!isset($_SESSION['User'] || !is_object($_SESSION['User'])) {
$_SESSION['User'] =3D new User();
$message =3D sprintf( "jqz cbig90--\$hits =3D=3D %d.", $hits);
system_error( $message);
}
if(!is_object($_SESSION['User'])) {
$message =3D sprintf( "jqz cbig93--\$hits =3D=3D %d.", $hits);
system_error( $message);
system_error( "Unable to instan_tiate User object." );
}

-- =

Rik Wasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация