Gayner Posted November 29, 2009 Share Posted November 29, 2009 Like I have a row in my table called "views' and in my php code When i view my prayer, i want it only increment my views using this code: update table set views=views+1 where id=$id But only once per session for each prayer :-) So people don't spam views up u know ? Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/ Share on other sites More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 you mean like this ? session_start(); if(empty($_SESSION['viewed'])){ $_SESSION['viewed'] = true; mysql_query("update table set views=views+1 where id=$id"); } Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/#findComment-967443 Share on other sites More sharing options...
Gayner Posted November 29, 2009 Author Share Posted November 29, 2009 will this work if(empty($_SESSION['viewed'])){ $_SESSION['viewed'] = true; echo "hey"; mysql_query("UPDATE prays set views=views+1 WHERE id={$row['id']}"); } else echo "already viewed"; I am sorry but i am so lost on sessions, how'd u get $_SESSION['viewed'] can we just create our own names for sessions? lmao cause right now it doesn't update i got over 200 prayers rows how does this work for each one seperately? Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/#findComment-967447 Share on other sites More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 Where does $row['id'] come from ? Think of sessions as a global array. you should have something like this session_start(); if(!empty($_GET['debug'])) $_SESSION['viewed'] = false; //debug if(empty($_SESSION['viewed'])){ $_SESSION['viewed'] = true; echo "hey"; $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']); mysql_query($SQL) or die($SQL."\n".mysql_error()); }else{ echo "already viewed"; } pass &debug=true in the URL to override the "already viewed" check Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/#findComment-967475 Share on other sites More sharing options...
Gayner Posted November 29, 2009 Author Share Posted November 29, 2009 Where does $row['id'] come from ? Think of sessions as a global array. you should have something like this session_start(); if(!empty($_GET['debug'])) $_SESSION['viewed'] = false; //debug if(empty($_SESSION['viewed'])){ $_SESSION['viewed'] = true; echo "hey"; $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']); mysql_query($SQL) or die($SQL."\n".mysql_error()); }else{ echo "already viewed"; } pass &debug=true in the URL to override the "already viewed" check rowid from my mysql $query="SELECT `id`,title,prayer,time,level,nameid,name,prays FROM prays WHERE id = {$_GET['view']} ORDER by id DESC"; $result=mysql_query($query); while($row=mysql_fetch_array($result)) { it's just id from table, i dont wanna use _GEt to override that will look ugly on the url, i know some sites have in built in.. Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/#findComment-967652 Share on other sites More sharing options...
Gayner Posted November 29, 2009 Author Share Posted November 29, 2009 Yea the problem is if somone knows &debug=true then they will just spam away.... Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/#findComment-967654 Share on other sites More sharing options...
Gayner Posted November 29, 2009 Author Share Posted November 29, 2009 Like I have a row in my table called "views' and in my php code When i view my prayer, i want it only increment my views using this code: update table set views=views+1 where id=$id But only once per session for each prayer :-) So people don't spam views up u know ? Lol ur Script didn't even help me people just used &Debug=true to keep spamming. here is the final product, works flawless, reduces 80% of views // anti flood protection if(isset($_SESSION['lol']) && $_SESSION['lol'] > time() - 3){ // users will be redirected to this page if it makes requests faster than 2 seconds echo ""; exit; } else echo "hey"; $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']); mysql_query($SQL) or die($SQL."\n".mysql_error()); mysql_close(); $_SESSION['lol'] = time(); } WIN WIN! Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/#findComment-967657 Share on other sites More sharing options...
MadTechie Posted November 30, 2009 Share Posted November 30, 2009 The debug, is for "debugging" to allow you force an update, your final code is basically the same as the first code I posted but works per 3 seconds not per session as requested! Pssst your missing a { Quote Link to comment https://forums.phpfreaks.com/topic/183294-views-counter-only-add-this-once-each-time-not-refresh/#findComment-967773 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.