subhomoy Posted March 14, 2012 Share Posted March 14, 2012 hii i want to create a script where i want to store unique page views per 24 hrs using cookie.. i had create a code but rectify me if i'm wrong... 1) i wan to store a cookie so i set this code on top to create a cookie on views browser.. <php setcookie("user", time()+84600); ?> 2) Now i want to find that if the cookie was set before, then the views will not update, if not then it will increase by 1. <php if (isset($_cookie["user"])) echo "Total no of views" . $views . "; else echo" $views=$views+1; echo " $views"; $k=mysql_query("UPDATE `images` SET views = '$views' WHERE id = '$userid'") ?> but the problem i'm facing is that where to put that "$views" so that it will increase and will show to the user... now suppose every thing works well then how the cookie will understand that the views will be updated in specific users database... Plz guys i really need help... If some other possible ways are there then plz refer... Thanks in advance.. Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/ Share on other sites More sharing options...
subhomoy Posted March 15, 2012 Author Share Posted March 15, 2012 any help guysss Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/#findComment-1327555 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 Currently, your cookie is only considered a "session cookie". That is, it will expire as soon as you close the browser. Since 24 hours probably won't have passed before the user next opens their browser, this is not acceptable. You'll need to tell the cookie to expire in 24 hours. You have the right idea, but you put it in the wrong parameter. The expire time is the third parameter. So just change it to: setcookie("user", "", time()+84600); To update the views, this should do: $k=mysql_query("UPDATE `images` SET views = views+1 WHERE id = '$userid'"); I don't know where/how $userid is defined, but make sure it can't contain SQL injection. If it is numerical you can simply cast it to an int to make sure. $userid = (int) $userid; Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/#findComment-1327656 Share on other sites More sharing options...
subhomoy Posted March 15, 2012 Author Share Posted March 15, 2012 @scootstah Thanks it was really helpfull Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/#findComment-1327715 Share on other sites More sharing options...
Monkuar Posted March 15, 2012 Share Posted March 15, 2012 Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also. Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what? Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/#findComment-1327731 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also. Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what? The cookie has no value, it simply exists. Basically if it exists, don't add a view, if it doesn't exist create it and add a view. Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/#findComment-1327740 Share on other sites More sharing options...
Monkuar Posted March 15, 2012 Share Posted March 15, 2012 Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also. Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what? The cookie has no value, it simply exists. Basically if it exists, don't add a view, if it doesn't exist create it and add a view. How would you differentiate between each post/image they're viewing? I am trying to do the same thing for a system of mine. If a user has viewed the topic in less then 24hours, store the id's in a array, and if it exists, echo NO SQL to update+view, would that be the same case here? Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/#findComment-1327801 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also. Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what? The cookie has no value, it simply exists. Basically if it exists, don't add a view, if it doesn't exist create it and add a view. How would you differentiate between each post/image they're viewing? Right, I guess I was just thinking of the website itself and not individual pages. I am trying to do the same thing for a system of mine. If a user has viewed the topic in less then 24hours, store the id's in a array, and if it exists, echo NO SQL to update+view, would that be the same case here? There's a lot of different ways to do it, but most of them result in unnecessary database reads/writes. For something simple like viewing a page and adding a view every 24 hours, it's pretty easy. You can just keep an array in a cookie or something with a last_visited timestamp. For something like a forum where it becomes "unread" after other activities, it's a little more complicated. Quote Link to comment https://forums.phpfreaks.com/topic/258922-page-views/#findComment-1327818 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.