ababba2 Posted December 17, 2015 Share Posted December 17, 2015 (edited) Hi guys. I need to make a view counting script for my site. Using mysql. I tried with the following code. But I don't know why it doesn't work. <?php $refreshed = $_SERVER['HTTP_CACHE_CONTROL']; if ($refreshed == 'max-age=0'){ $db = JFactory::getDBO(); $query = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id=" . $details->id; $db->setQuery($query); $db->query(); }?> Using this code, views doesn't grow up. And if possible, I would like to set +1 in the database at every refresh of the page. Could someone give me help? Thank you EDIT: of course __hdflv_upload already exist in the database Edited December 17, 2015 by ababba2 Quote Link to comment Share on other sites More sharing options...
unistake Posted December 17, 2015 Share Posted December 17, 2015 At the top of the page I'd just get the number of counts currently sorted in the db then add one. $count = $row['count']; $new_count = $count + 1; $sql= "UPDATE table SET count = '$new_count' WHERE id='$details'"; $result = mysqli_query($cxn,$sql); Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 17, 2015 Share Posted December 17, 2015 My phpinfo does not show an element called HTTP_CACHE_CONTROL - does yours? I realize that you are trying to avoid repeated F5 hits here but is it really necessary. Why not just trigger "new" hits on a page with a snippet of code and a function that logs the data? I woule just check if there existed a submit value that comes from the current page; if it doesn't then it is a new visit to the page and I would log it. If not, I wouldn't. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 17, 2015 Share Posted December 17, 2015 (edited) At the top of the page I'd just get the number of counts currently sorted in the db then add one. $count = $row['count']; $new_count = $count + 1; $sql= "UPDATE table SET count = '$new_count' WHERE id='$details'"; $result = mysqli_query($cxn,$sql); There is no need to run a SELECT query to get the current count in order to increment it by one before running the UPDATE query. You can do it all within an UPDATE query UPDATE table SET field=field+1 WHERE id=$id Edited December 17, 2015 by Psycho 2 Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 I cannot use other tables... I need to use __hdflv_upload and times_viewe for many reason... Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 18, 2015 Share Posted December 18, 2015 I cannot use other tables... I need to use __hdflv_upload and times_viewe for many reason... No one is suggesting to use another table - those were examples. Did you even read ginerjm's response? How do you know that there is a HTTP_CACHE_CONTROL value? You need to add code for debugging purposes. <?php if(!isset($_SERVER['HTTP_CACHE_CONTROL'])) { echo "HTTP_CACHE_CONTROL doesn't exist"; } elseif($_SERVER['HTTP_CACHE_CONTROL'] != 'max-age=0') { echo "HTTP_CACHE_CONTROL doesn't equal 'max-age=0'. It is '{$_SERVER['HTTP_CACHE_CONTROL']}'."; } else { $db = JFactory::getDBO(); if(!$db) { echo "Unable to create DB reference"; } elseif(!$details->id) { echo "Details id does not exist."; } else { $query = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id={$details->id}"; $db->setQuery($query); if(!$db->query()) { echo "Error executing query. Follow Joomla documentation to output the error."; } else { echo "Success"; } } } ?> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted December 18, 2015 Share Posted December 18, 2015 Had this for someone else, can use in any way need to with tracking clicks or views, Iframe,include,ajax. With a js onclick event or simple href link. Could change it around to add views or clicks each users name or user id, matter of editing it. http://forums.phpfreaks.com/topic/296442-unique-click-tracking-using-php/?p=1512504 Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 Sorry, it was my mistake I don't have HTTP_CACHE_CONTROL anyway I don't want to track unique IP/Views I would like to have +1 views at every refresh/page open Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 18, 2015 Share Posted December 18, 2015 In that case, I already gave you my $.02 Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 (edited) Thank you, now the script works. Anyway I'm facing another problem. If I set the joomla cache of my site Off, then at every refresh I get +1. Anyway, If I turn On the cache plugin, then I get +1 the first time I load the page and then it stop counting. I need to clear cache for get another +1 in the view count of that page. From admin panel I see that every other page reload doesn't get count by the script. How can I fix this? I thought maybe I could disable caching for this little script. There are many plugin for this in Joomla, but I have no idea how to do this in php. Edited December 18, 2015 by ababba2 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 18, 2015 Share Posted December 18, 2015 I don't know how you are doing this that makes the cache affect you. I do this all the time. Every time my script executes it checks if it has already been run and is processing a response, or if it is the "first" time it has been called. If it is the "first" then I increment my counter. Obviously this won't tally up refreshes, but why would you want to do that? It doesn't tell you anything about usage. So - show us your code for keeping your hit counts so we can see why the cache is affecting you. Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 I used Psyco's script... I didn't changed anything in his script you see above... I just removed this if(!isset($_SERVER['HTTP_CACHE_CONTROL'])) { echo "HTTP_CACHE_CONTROL doesn't exist"; } elseif($_SERVER['HTTP_CACHE_CONTROL'] != 'max-age=0') { echo "HTTP_CACHE_CONTROL doesn't equal 'max-age=0'. It is '{$_SERVER['HTTP_CACHE_CONTROL']}'."; } else { Anyway, I'm getting this cache problem Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 18, 2015 Share Posted December 18, 2015 To be accurate - you haven't posted YOUR code. Humor us and show us what you have typed up so that we can be sure that we are looking at your running code. Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 (edited) <?php $db = JFactory::getDBO(); if(!$db) { echo ""; } elseif(!$details->id) { echo ""; } else { $query = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id={$details->id}"; $db->setQuery($query); if(!$db->query()) { echo ""; } else { echo ""; } } ?> This is working. Edited December 18, 2015 by ababba2 Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 (edited) ^Is working fine, but as I said there is cache problem. I found this old quote on another forumt that could be useful Problem is that Joomla counts and update the article hits when the page is built, but if the caching plugin is enabled and the page has been cached to disk, the page is loaded directly from the cache directory, without updating the number of views. but still no solution Edited December 18, 2015 by ababba2 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 18, 2015 Share Posted December 18, 2015 That code is pretty weak looking. You test two things to see if they failed, but when they do you don't output anything to tell you they failed. Echo-ing out an empty string as you do is not very helpful when something didn't work. And if it didn't work you continue on with the rest of the script anyway. Why? To top it all off - you then do your update query and once again you test for failure but don't put out a message whether it actually updated or not. As for caching - how does a php script get cached on the browser since it only exists on the server? Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 (edited) Of course before removing the text inside the echo I tried this code too <?php $db = JFactory::getDBO(); if(!$db) { echo "Unable to create DB reference"; } elseif(!$details->id) { echo "Details id does not exist."; } else { $query = "UPDATE __hdflv_upload SET times_viewed=1+times_viewed WHERE id={$details->id}"; $db->setQuery($query); if(!$db->query()) { echo "Error executing query. Follow Joomla documentation to output the error."; } else { echo "Success"; } } } ?> With cache off, as result of the script I get "Success". And it's true.With cache on, as result I get "success". But from Admin panel I see that isn't true. For caching... well. Browser caching is disabled. I don't mean that kind of cache. I mean this kind of cache: https://docs.joomla.org/Cache Edited December 18, 2015 by ababba2 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 18, 2015 Share Posted December 18, 2015 So then perhaps you should direct this topic to a CMS forum, or even a Joomla one. Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 18, 2015 Author Share Posted December 18, 2015 (edited) Well, I don't think I will get any solution there. Usually they don't offer this kind of support... It's not the first time I ask them support for advanced problems and I never had answer... Anyway I found this article on google: http://www.inmotionhosting.com/support/edu/joomla-3/increase-hits-cache This explain how the fix view counter on articles when cache enabled. But my script is not based on article views. It's a different kind of view counter. But maybe could be useful for understand how to fix even my problem... They say to use jquery... do you think thi can be useful? Edited December 18, 2015 by ababba2 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 19, 2015 Share Posted December 19, 2015 We need to do some definitions clearing up here. Your article talks about "article" hits. Can you define for me what pages you are attempting to count? If you are displaying a requested document (not a script) and want to have it counted every time it is requested, that is definitely not what we(?) have been giving you. Of course I can see why joomla as well as a browser would give you the cached copy instead of a new one. So - when the user clicks on whatever you have on your webpage in order to see a new document, what actually happens? Is there a form submit to a php script? Or at least an anchor href pointing to a php script? Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 19, 2015 Author Share Posted December 19, 2015 (edited) http://test995.altervista.org/This is a testing site I use for test. As you can see in that site, there is a video gallery. What I'm trying to do is to count video view. For "count video view" I don't mean how many times someone click play on the video. I mean how many time a page that cointains a video is opened by someone Edited December 19, 2015 by ababba2 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 19, 2015 Share Posted December 19, 2015 Some of us don't just click on links provided in posts. Some of us want specific answers to question or specific code that has a problem. Maybe you could answer my last question instead of posting whatever you just posted. Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 20, 2015 Author Share Posted December 20, 2015 I'm not native speaker, so it's hard for me explain in detail. Please try to understand. Your article talks about "article" hits. Can you define for me what pages you are attempting to count? I want to count how many time a page that cointains a video is opened by someone If you are displaying a requested document (not a script) and want to have it counted every time it is requested, that is definitely not what we(?) have been giving you. Sorry, I don't understand what you mean with document. If you mean excel files, word files, zip files... Then no, it's not a document. So - when the user clicks on whatever you have on your webpage in order to see a new document, what actually happens? Is there a form submit to a php script? Or at least an anchor href pointing to a php script? In homepage there is a list of videos. When you click on the video you want to see, happens that a php script runs and show a page containing that videos. I would like to count this kind of view... Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 20, 2015 Share Posted December 20, 2015 What handles that click action? You say that a "php script runs". So? Why doesn't that script update the count? Quote Link to comment Share on other sites More sharing options...
ababba2 Posted December 20, 2015 Author Share Posted December 20, 2015 Inside the php script there is a call to a flash file. This flash file handles many thing. For example, the player of the videos is based on flash. And also handles and update the counter view. But since flash is an outdated plugin, now I'm working for remove flash completely from my site. I replaced flash player with html5 player. Html5 player works fine, but now the problem is that the view counter doesn't work anymore since I removed the flash call. So I would like to make another script (not based on flash) that could update the view counter. But this shouldn't be affected by the joomla cache... Quote Link to comment 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.