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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session variable problem... 1

Status
Not open for further replies.

wagnj1

Programmer
Joined
Jul 21, 2003
Messages
154
Location
CA
I'm trying to get session variables working with my PHP v4.3.6. I know its pretty much built in with that version, but for some reason I'm having problems (its probably my ineptitude). I'm trying to get a simple counter working, here's what my code looks like:

<?php
session_start();
if (!session_is_registered('count')) {
session_register('count');
$count = 1;
} else {
$count++;
}
?>

<p>
Hello visitor, you have seen this page <?php echo $count; ?> times.
</p>

<p>
To continue, <a href="nextpage.php?<?php echo strip_tags(SID); ?>">click
here</a>.
</p>

For some reason, my count variable is always unregistered after I refresh or go back to the page later.
It appends the SID to the end of the link thats shown when you move the mouse over the 'next page' link, and this SID is always changing, shouldn't it be the same for the same user??

When I took office, only high energy physicists had ever heard of what is called the
Worldwide Web.... Now even my cat has its own page.
-Bill Clinton (1946 - ), announcement of Next Generation Internet initiative, 1996
 
Registering session variables with session_register() and then trying to use them as global variables is problematic, most commonly because session_register() requires that register_globals be set to "on" (See FAQ434-2999).

I strongly recommend that you not use session_register() and singleton session variables in the global namespace but rather direct manipulation of the $_SESSION superglobal array:

Code:
<?php
session_start();

if (isset($_SESSION['count']))
{
	$_SESSION['count']++;
}
else
{
	$_SESSION['count'] = 1;
}

print $_SESSION['count'];
?>



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That works like a fish!! Thanks for the good advice!!

When I took office, only high energy physicists had ever heard of what is called the
Worldwide Web.... Now even my cat has its own page.
-Bill Clinton (1946 - ), announcement of Next Generation Internet initiative, 1996
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top