Click to See Complete Forum and Search --> : $count++ not working - OR $count = $count + 1;


Turboz
05-30-2005, 08:46 AM
Hi all

I'm using this code:
<?php #start a session
session_start();

#register a session variable called count
session_register("count");

#increment the value of the counter variable
$count++;

#display the current value of the count variable
$msg = "You have visited this page $count times ";
$msg.= "in this session.";
echo( $msg );

?>



I'm testing it on my local server. For some reason the damn thing will not work. Each time I refresh the page it continually says 1.

On the live server it works perfectly with no problems.

Can someone please tell me whats wrong with my php/apache installation thats causing this?

Thankyou,

-Turboz

thorpe
05-30-2005, 12:13 PM
register globals is turned off, and no, there is nothing wrong with this setup. in fact it is recommended.

abc
05-30-2005, 12:28 PM
Hi

Something like so...


<?
session_start ();

if ( ! isset ( $_SESSION['count'] ) )
{
$_SESSION['count'] = 0;
}

$count = ( $_SESSION['count'] += 1 ); // I do it this way to convert the long session name to 'count' and change the session value at the same time!

$msg = "You have visited this page " . $count . " times ";

$msg .= "in this session.";

echo $msg;
?>


I would just use the session name, as there is no need to use more memory for nothing. I only did it to show you how to shorten the $var name...

Like I said I would just do this!


<?
session_start ();

if ( ! isset ( $_SESSION['count'] ) )
{
$_SESSION['count'] = 0;
}

$_SESSION['count'] += 1;

echo "You have visited this page " . $_SESSION['count'] . " times in this session.";

?>



abc

Turboz
05-30-2005, 01:16 PM
abc - Thanks for that - it worked a treat.

Just one thing though I'm confused.

According to things I've found on the web, $count = $count + 1; should of worked.

Whats wrong with that and why does it only work with $count += 1; ??


Originally posted by thorpe
register globals is turned off, and no, there is nothing wrong with this setup. in fact it is recommended.

Yes I worked out that the register globals were turned off as every other site seemed to suggest which I had searched. The only problem is I originally tried abc's method and it didn't seem to work. Of course I didn't realise that $_session and $_SESSION were different and only one would work doh!

Oh, thorpe one other thing; register_globals is turned off on my live site too - yet the original version of the script (at the top of this thread) worked perfectly for some reason. Now am I paranoid or is there something else that could be affecting it?

Sorry for all the questions but I'm new to php and still trying to work out the basics!

-Turboz