king.oslo Posted May 31, 2009 Share Posted May 31, 2009 Hello my friends. I am just learning about sessions and I am trying to make a script that counts and prints the number of visits a user makes each session. My problem is that when I refresh the page for the first time and expecting the counter to go from "1 time" to "2 times" the only thing that happens is that the counter goes from "1 time" to "1 times". Please let me know what is wrong, and why Thanks <?php session_start(); if (isset($_SESSION['counter']) ){ $counter = $_SESSION['counter']++; $type = ' times'; } else { $counter = $_SESSION['counter'] = 1; $type = ' time'; } print 'You have visited this web site ' . $counter . $type . ' this session.' ?> Quote Link to comment https://forums.phpfreaks.com/topic/160405-solved-problem-with-simple-script/ Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Change $counter = $_SESSION['counter']++; To $counter = ++$_SESSION['counter']; Quote Link to comment https://forums.phpfreaks.com/topic/160405-solved-problem-with-simple-script/#findComment-846445 Share on other sites More sharing options...
king.oslo Posted June 1, 2009 Author Share Posted June 1, 2009 Change $counter = $_SESSION['counter']++; To $counter = ++$_SESSION['counter']; Hey man, that worked! Whats the logic behind it? I like your signature by the way.oz Quote Link to comment https://forums.phpfreaks.com/topic/160405-solved-problem-with-simple-script/#findComment-846502 Share on other sites More sharing options...
.josh Posted June 1, 2009 Share Posted June 1, 2009 order of operation... Quote Link to comment https://forums.phpfreaks.com/topic/160405-solved-problem-with-simple-script/#findComment-846506 Share on other sites More sharing options...
king.oslo Posted June 1, 2009 Author Share Posted June 1, 2009 order of operation... Hey, I thought so too, but I did not think that moving the ++ further forward would change anything about the order of operation. How come?oz Quote Link to comment https://forums.phpfreaks.com/topic/160405-solved-problem-with-simple-script/#findComment-846515 Share on other sites More sharing options...
.josh Posted June 1, 2009 Share Posted June 1, 2009 http://us2.php.net/operators.increment Quote Link to comment https://forums.phpfreaks.com/topic/160405-solved-problem-with-simple-script/#findComment-846524 Share on other sites More sharing options...
king.oslo Posted June 1, 2009 Author Share Posted June 1, 2009 http://us2.php.net/operators.increment Thanks, I get it now oz Quote Link to comment https://forums.phpfreaks.com/topic/160405-solved-problem-with-simple-script/#findComment-846548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.