Salonica Posted November 25, 2006 Share Posted November 25, 2006 Hi all,Wonder if anyone could help with the following problem i'm having.Ok I have a script and am trying to modify the bit that updates the databasewith the number of times the page has been viewed.In the script at present every time the page is reloaded then the count incrementsby one. Obviously that brings up the problem of the page just getting reloaded toincrease the number of hits so I want it to check the session so that it would updateonly once even if the same person refreshed the page several times.At the top of the script it calls in a function to start a session as this is required forother aspects of the site then towards the end it has the following that updatesthe database:$UP = "UPDATE $ads_tbl SET sitehits = sitehits + 1 where siteid = '{$_GET['sid']}'";mysql_query ($UP) or die(mysql_error());} else { echo "<td>$error</td></tr></table></td></tr></table>";}How would i change the above to prevent reloads adding to the count??Any help appreciated ???CheersSteve Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/ Share on other sites More sharing options...
onlyican Posted November 25, 2006 Share Posted November 25, 2006 I would do something likeif(!$_SESSION["onsite"]){//run your code to add to the site stats$_SESSION["onsite"] = true;} Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130169 Share on other sites More sharing options...
Salonica Posted November 26, 2006 Author Share Posted November 26, 2006 Hi,Many thanks for the prompt reply - that does work but when you visit a different page then it won't update the count on that page because the session is in place still. Any ideas how would I get round this ?CheersSteve Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130366 Share on other sites More sharing options...
JasonLewis Posted November 26, 2006 Share Posted November 26, 2006 you could do something like this, it involves arrays as well.[code=php:0]session_start();if(!isset($_SESSION['page'])){ $_SESSION['page'][] = $_SERVER['REQUEST_URI']; }if(!in_array($_SERVER['REQUEST_URI'], $_SESSION['page'])){//UPDATE THE HITS$_SESSION['page'][] = $_SERVER['REQUEST_URI'];}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130372 Share on other sites More sharing options...
corbin Posted November 26, 2006 Share Posted November 26, 2006 If you wanted to recount the same user after x minutes you could do something like the following:(at the top of the page)[code=php:0]$minvar = 5; //how many minutes you wish to elapse before a user is recounted.$UP = "UPDATE $ads_tbl SET sitehits = sitehits + 1 where siteid = '{$_GET['sid']}'"; // your update queryif(!$_SESSION['lutime']) { //sets the var if its not set.$_SESSION['lutime'] = time();mysql_query ($UP) or die(mysql_error()); // if the variable isnt set that means the user is new, so update}elseif(($_SESSION['lutime'] + ($minvar*60)) < time()) { //if the last updated time + seconds * minvar > the current time, updatemysql_query ($UP) or die(mysql_error()); // its been x minutes since the user was counter, so count them$_SESSION['lutime'] = time();} [/code]Hmm that should work (if you wish to use it). It might not work though (writtin in the little quick reply box at 1 AM)...(edited to reflect the following post.) Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130391 Share on other sites More sharing options...
JasonLewis Posted November 26, 2006 Share Posted November 26, 2006 correct me if i'm wrong but shouldn't this:[code=php:0]($_SESSION['lutime'] + ($minvar*60)) > time()[/code]be this:[code=php:0]($_SESSION['lutime'] + ($minvar*60)) < time()[/code]because its always going to be greater then time() if you add it. so when time() catches up to it and becomes greater then it we can run the code and update it again. Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130398 Share on other sites More sharing options...
corbin Posted November 26, 2006 Share Posted November 26, 2006 Hehe youre right... For some reason I use time comparisons like that a lot and I always manage to get 'em backwards... Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130403 Share on other sites More sharing options...
JasonLewis Posted November 26, 2006 Share Posted November 26, 2006 yeh it can be a bit confusing, i used to screw it up a lot to. Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130412 Share on other sites More sharing options...
corbin Posted November 26, 2006 Share Posted November 26, 2006 Mainly just that time situation always gets me >.<Been coding PHP for almost 2 years now... Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130413 Share on other sites More sharing options...
Salonica Posted November 26, 2006 Author Share Posted November 26, 2006 Wow - many thanks everyone for your replies ;DI went with the ProjectFear version and it works great butappreciate the other options offered too!This will stop 'em refreshing the pages to show more hits ;)Thanks again everyone!CheersSteve Quote Link to comment https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130433 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.