Page 1 of 1

PHP Code Question

Posted: Mon Mar 13, 2006 8:29 am
by Mitchell
Ok well I am in midst of learning php. Mostly php in game programming.
The book I am using is: PHP Game Programming by Matt Rutledge

So I got into chapter1 which tells me to type up this code and it should keep track of how many times you have accessed the page. I keep getting errors and am just wondering if its my machine.

So if you could help me out by checking it and or offering advice that would be wonderful!

This is the page: Example01

Here is the code:

Code: Select all

<?php
if(!session_is_registered('nCount'))
{
    session_register('nCount');
    $nCount = 1;
}
else
{
    $nCount++;
}
?>
echo'
<p>Hello, you have seen this page <?php echo $nCount; ?> times.</p>

<p>To continue, <a href="somepage.php?<?php echo strip_tags(SID)?>;">click here (sompage.php?<?php echo strip_tags(SID)?>)</A></p>
The actual page code is:

Code: Select all

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
       <title>Chapter 1 Example. Figure 1.2</title>
</head>
<body>
<?php
if(!session_is_registered('nCount'))
{
    session_register('nCount');
    $nCount = 1;
}
else
{
    $nCount++;
}
?>
echo'
<p>Hello, you have seen this page <?php echo $nCount; ?> times.</p>

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

</body>
</html>
I keep getting an error on line 10 which is session_register('nCount');

Posted: Mon Mar 13, 2006 9:44 am
by Michael_C
I don't know php, but would think you'd want the variable to exist prior to registering it.

So I would try:

Code: Select all

$nCount = 1; 
    session_register('nCount');
Michael

Posted: Mon Mar 13, 2006 9:50 am
by Michael_C
I believe you'll need to initialzie session data by issuing

Code: Select all

session_start()
check out http://us3.php.net/manual/en/function.session-start.php

Posted: Mon Mar 13, 2006 9:54 am
by shiznatix
dont use sesson_register() or those functions. use the $_SESSION superglobal

Code: Select all

session_start();

$_SESSION['thingy'] = 'whoa nelly';

echo $_SESSION['thingy'];
thats a simple example