thara Posted September 23, 2012 Share Posted September 23, 2012 Hi all, Im looking for some help with a page views counter in php. suppose when user viewing a profile page and spend minimun 10 seconds time on that page then i have to calculate view as 1... so how to do that?? I have stored all profile details in a database and all pulled through the same template page, like profile.php?id=12345. can anybody give me sample example Thank you.. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 23, 2012 Share Posted September 23, 2012 You'll need to use Javascript and AJAX for this. SetTimeout () should be a good start for the JS end. However, I don't think it's a good idea, as how long a page is open doesn't say anything about whether or not it has actually been read (which I assume is the point of this idea). For example, I routinely have lots of tabs open in my browser. Some stuff is stuff I'm going to read later, some of it for reference, and some just because I'm too lazy to close them (). Also, there's been times where I've read the content, and closed the page, before spending 10 seconds on the site. Not to mention those who don't have JS (activated). That's why I recommend that you increase the counter when you're actually displaying the page. Lot less complexity needed, and will give a more accurate picture of the situation. You'll still don't know how many times the page has actually been read, but that you will never know anyway. Quote Link to comment Share on other sites More sharing options...
thara Posted October 8, 2012 Author Share Posted October 8, 2012 I tried with this script to counts how many users have viewed in my website's pages. This is my code <?php require_once ('test.php'); $institute_id = 14; $q = "INSERT INTO page_views2 ( institute_id, views) VALUES ( $institute_id, 1) ON DUPLICATE KEY UPDATE views=views+1" ; $r = mysqli_query ($dbc, $q); ?> I added this to top of my webpages and this is working (views incrementing) properly when I open the pages. But my problem is When I refresh the page, page views is incrementing by 1. It is okey when it is open first time. But I want to avoid from this when someone is refreshing the page. so can any body tell me how can I do this? any comments are greatly appreciated. thank you. Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 8, 2012 Share Posted October 8, 2012 if the user refresh tha page that should encremenr because that is considered as views. but if you dont like it then on your table where you have the counter add another field for the current page so if the current page save there is index.php it should not increment since thats also the last page visited. or store on session the last page visited Quote Link to comment Share on other sites More sharing options...
thara Posted October 8, 2012 Author Share Posted October 8, 2012 thanks for reply... I tried it with cookies... and this is my code... <?php require_once ('test.php'); $institute_id = 14; //if no such cookie exists, assume that its their first time viewing. if(!isset($_COOKIE['institute_id'])){ $q = "INSERT INTO page_views2 ( institute_id, views) VALUES ( $institute_id, 1) ON DUPLICATE KEY UPDATE views=views+1"; $r = mysqli_query ($dbc, $q); //set cookie saying they've viewed this institutions page. setcookie( 'institute_id', $institute_id, time()+1800); } ?> this is working.. when a user open a page and views increment by 1. then It is valid for 30 minutes from the time page opened. Can anybody tell me this is a good solution for me or is there any good solution for this than my solution? any comments are greatly appreciated. Thank you. Quote Link to comment Share on other sites More sharing options...
MDCode Posted October 8, 2012 Share Posted October 8, 2012 (edited) If a user were to clear their cookies they could simply log in and view the page again, thus incrementing it as many times as they want Edited October 8, 2012 by ExtremeGaming Quote Link to comment Share on other sites More sharing options...
thara Posted October 10, 2012 Author Share Posted October 10, 2012 I used this script to count page views in my project. It is working. But when I open several pages on several tabs in same browser its incrementing only one page and others not incrementing. Can anybody tell what is the reason? This is my script.. <?php require_once ('test.php'); $institute_id = 14; //if no such cookie exists, assume that its their first time viewing. if(!isset($_COOKIE['institute_id'])){ $q = "INSERT INTO page_views2 ( institute_id, views) VALUES ( $institute_id, 1) ON DUPLICATE KEY UPDATE views=views+1"; $r = mysqli_query ($dbc, $q); //set cookie saying they've viewed this institutions page. setcookie( 'institute_id', $institute_id, time()+1800); } ?> Thanks you.. 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.