PHP Code Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mitchell
Forum Newbie
Posts: 1
Joined: Mon Mar 13, 2006 8:07 am

PHP Code Question

Post 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');
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post 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
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
Post Reply