Forum Moderators: coopster

Message Too Old, No Replies

PHP and Cookies Issue

         

username

7:22 am on Jul 7, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all, I am setting up a site which has various news stories across the site, all of which have a 'views' value which detects if a particular user has viewed the article. I am checking this using a cookie, but I am getting a strange issue. When a user views the article for the first time, 3 views instead of 1 shows up. 1 at first when you visit the page then a count of 3 when you leave the page. My code is below:

if (isset($_COOKIE['ArticleCookie'])) {
//cookie is set: do nothing
} else {
setcookie("ArticleCookie", "$categoryid-$articleid", time()+3600*24*15);
$views->views($articleid, $categoryid); //views + 1 function
}

This code is at the top of each article page when the page runs. I cannot figure out why it is giving 3 as the value after it shows 1 once I leave the page?

Your help would be appreciated. Thanks in advance.

phparion

10:58 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



your code looks good and is hiding views() function body. You must be making mistake in either this function body in incrementing or you are calling the same function three times.

I will suggest you to put different check points in your code. e.g put an echo in your else {} to see how many times that is echoed. if that line is echoed three times it means your code is called three time. If that is echoed one time then it means your views() function has incrementing by 3 instead of 1.

dreamcatcher

11:30 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you echoed the values of $categoryid & $articleid to see they hold the correct values?

dc

username

10:37 pm on Jul 7, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for the help guys, but PHP weirdom has struck. It turns out incrementing the views value using $views++ was causing the issue. I changeed it to $viewsNew = $views + 1; and the problem went away?

Thanks for the help anyway!

penders

11:26 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It turns out incrementing the views value using $views++ was causing the issue. I changeed it to $viewsNew = $views + 1; and the problem went away?

I think there is something else going on here? Perhaps a variable type issue?

Are you sure $views is an integer? ...In your original post, $views appears to be an instance of a class?

username

11:52 pm on Jul 7, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Sorry, I should have clarified, $views++ was the variable in the views() function. It's all working now.