By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

Having Trouble with a Basic Hit Counter

P: n/a
I've got a piece of PHP code that gets executed twice. The first time
through, variable <$Useris 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 <$hitsto 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 <$hitsshould 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 <$hitsis 1 but the second
time through this section <$hitsis 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_erroris just:

################################################## ##################

function system_error( $error ) {
die
( "<div style=\"font: normal 12px verdana;\"><b>System Error:</b>
"
. $error . "</div>" );
}

Jul 19 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rik
On Thu, 19 Jul 2007 17:25:16 +0200, <kv******@hotmail.comwrote:
I've got a piece of PHP code that gets executed twice. The first time
through, variable <$Useris 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 <$hitsto 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 <$hitsshould 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 <$hitsis 1 but the second
time through this section <$hitsis 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 = 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
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'] = 0;
$_SESSION['hits']++;

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

--
Rik Wasmus
Jul 19 '07 #2

P: n/a
Rik
On Thu, 19 Jul 2007 17:56:57 +0200, Rik <lu************@hotmail.comwrote:
session_start();
if(!isset($_SESSION['hits']) $_SESSION['hits'] = 0;
$_SESSION['hits']++;

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


--
Rik Wasmus
Jul 19 '07 #3

P: n/a
kv******@hotmail.com wrote:
I've got a piece of PHP code that gets executed twice. The first time
through, variable <$Useris 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 <$hitsto 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 <$hitsshould 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 <$hitsis 1 but the second
time through this section <$hitsis 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_erroris 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.
js*******@attglobal.net
==================
Jul 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.