Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

undefined variable???

Status
Not open for further replies.

jemminger

Programmer
Jun 25, 2001
3,453
US
hi all,

hope you can help...this is driving me crazy:

i've written this page:
[tt]
<?php
session_start();
session_register('hits');
++$hits;
?>
this page has been viewed <?php echo $hits ?> times.
[/tt]

when i run it, i get this error the first time:
&quot;Warning: Undefined variable: hits in c:\inetpub\ on line 4&quot;

what's the deal? i've defined &quot;$hits&quot; in line 3!
=========================================================
if (!succeed) try();
-jeff
 
Maybe, maybe not.

If you have the PHP configuration directive &quot;register_globals&quot; set to &quot;off&quot;, the only way to access that variable is as $_SESSION[&quot;hits&quot;] ______________________________________________________________________
TANSTAAFL!
 
hi sleipnir214,

i have already added &quot;register_globals = on&quot; to php.ini, and according to phpinfo(), register_globals is &quot;on&quot;. any other ideas?
=========================================================
if (!succeed) try();
-jeff
 
Here's my theory: the registration of global variables only happens when the values are pulled from the store, not when they are created the way you originally did.

Basically, $_SESSION['hits'] exists as soon as you invoke session_register, but $hits is not created at that time.

Try this as a test:

<?php
session_start();
$hits=0;
session_register('hits');
++$hits;
?>
this page has been viewed <?php echo $hits ?> times.

______________________________________________________________________
TANSTAAFL!
 
sleipnir214,

thanks for the suggestion - yes, this will keep that error from displaying, but it also defeats the purpose of the script...the counter will never be greater than 1.

=========================================================
if (!succeed) try();
-jeff
 
You only use session_register() on any variable one time in your entire site. Once the variable is registered, it will be available with just session_start().

Try:

<?php
session_start();
if (!array_key_exists('hits', $_SESSION))
{
$hits=0;
session_register('hits');
}
++$hits;
?>
this page has been viewed <?php echo $hits ?> times. ______________________________________________________________________
TANSTAAFL!
 
Couldn't you also do something like this, might be a little easier:

<?php
if (isset($_SESSION['hits']) {
$hits = $_SESSION['hits'];
}
else {
$hits = 0;
}

++$hits;

session_start();
session_register('hits');
?>
this page has been viewed <?php echo $hits ?> times.


The reason you get that error message is because of a setting in your php.ini file which says display all messages (do a search in php.ini for E_ALL).

You'll also notice I put the ++$hits above the session_register otherwise you'll never increase your counter, I don't think anyway.

Hope this helps

--
Jon --
Jon
 
Valcor,

your suggestion won't work. Unless you have the PHP runtime configuration directive &quot;session.auto_start&quot; set to &quot;on&quot; $_SESSION isn't initialized until your script issues session_start(). So $_SESSION['hits'] will never be set in your code sample. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top