jdock1 Posted July 21, 2009 Share Posted July 21, 2009 The past 3 month I have been studying PHP non stop on my free time, probably about 3 hours a day. I am becoming pretty good at coding basic to some-what-advanced scripts, but I searched the manual and cant find a way to do this. Basically, what I want to find out is how to limit the surfer to view the site a certain number of times a day. Like I have a service, the surfer can use the service three times a day, if they visit the page a 4th time on the same day, it echos an error stating they have to wait or upgrade their plan. How could I go about doing this? I tried multiple things using counters, but not getting anywhere! Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/ Share on other sites More sharing options...
Amtran Posted July 21, 2009 Share Posted July 21, 2009 You could store the IP, along with the number of visits and time of last visit, in a MySQL table or text file. Then just have a cron job clear out all the entries that are more than 24 hours old. Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879280 Share on other sites More sharing options...
HPWebSolutions Posted July 21, 2009 Share Posted July 21, 2009 Does the user have to be logged in to look at the page? If so, you could store the number of times they looked at a particular page that day in a database, and stop them from looking at it anymore that day if they exceed the threshold. If they aren't going to be logged in, you could store a $_SESSION variable with data in it regarding the number of times the user looked at each page, and deny access to the page if they have looked at it too many times. The key will be checking these values before displaying any page. Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879282 Share on other sites More sharing options...
jdock1 Posted July 21, 2009 Author Share Posted July 21, 2009 Does the user have to be logged in to look at the page? If so, you could store the number of times they looked at a particular page that day in a database, and stop them from looking at it anymore that day if they exceed the threshold. If they aren't going to be logged in, you could store a $_SESSION variable with data in it regarding the number of times the user looked at each page, and deny access to the page if they have looked at it too many times. The key will be checking these values before displaying any page. I didnt even think of using sessions. But no, theyre just surfers that dont require a login. And I am trying to stay away from databases, because my sql server goes down alot plus I am not that great at sql Could you possibly give me some code examples? Thanks bro! Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879300 Share on other sites More sharing options...
Amtran Posted July 21, 2009 Share Posted July 21, 2009 You can't use sessions, as all the user would need to do is close the browser, and it would be destroyed. Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879303 Share on other sites More sharing options...
JonnoTheDev Posted July 21, 2009 Share Posted July 21, 2009 If you want a quick & dirty solution, then just set a cookie. Obviously the user could delete their cookies but if you don't have access to a database then your only other option is to use a flat file to record IP addresses. Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879304 Share on other sites More sharing options...
BMurtagh Posted July 21, 2009 Share Posted July 21, 2009 I think storing it in the db would be the best way to do it, but another way I thought of that may work would be to use flat files. Write their information to the text file and then parse the data. It avoids having to use your SQL db, but not sure how practical it would be. Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879305 Share on other sites More sharing options...
Amtran Posted July 21, 2009 Share Posted July 21, 2009 That's the third time it's been said... Take the bait! Do it! Go .txt! Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879307 Share on other sites More sharing options...
jdock1 Posted July 21, 2009 Author Share Posted July 21, 2009 That's the third time it's been said... Take the bait! Do it! Go .txt! Lol. I suppose your all talking about fopen? Well how would I implement that? I guess im much more of a newbie than I thought I was! Thanks for all your kind answers Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879323 Share on other sites More sharing options...
Q Posted July 21, 2009 Share Posted July 21, 2009 Example code snatched from php.net <?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879357 Share on other sites More sharing options...
jamesxg1 Posted July 21, 2009 Share Posted July 21, 2009 <?php $open = file('name.txt'); $ip = getenv("REMOTE_ADDR"); $num = 1; $num = $num + 01; $contents = $ip . '-' . $num . '\r'; file_put_contents("$open", $contents); Maybe something like so ? Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879373 Share on other sites More sharing options...
jdock1 Posted July 21, 2009 Author Share Posted July 21, 2009 <?php $open = file('name.txt'); $ip = getenv("REMOTE_ADDR"); $num = 1; $num = $num + 01; $contents = $ip . '-' . $num . '\r'; file_put_contents("$open", $contents); Maybe something like so ? Thanks, I got that, but I still dont know how to implement that. I tried using if statements, but nothing is working. I just need to find out how to check if the $ip has been on the page over $num times, in this case three, and if so, it echos a message. Can anybody help me out a little more? This seems to be the only place I can get the right answers Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-879871 Share on other sites More sharing options...
BMurtagh Posted July 22, 2009 Share Posted July 22, 2009 Hey, I think at this point it'd be best to paste the current code you're working with so people can take a look. It'll help you get on the right track quicker hopefully. Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-880045 Share on other sites More sharing options...
haku Posted July 22, 2009 Share Posted July 22, 2009 A flatfile IS a database. Learning PHP without learning how to use a database is pretty much pointless. You will reach a wall very fast (and by that I mean today) in what you can do. So you may as well buckle down and figure out how databases work. It's really not that hard to use basic database functions - no harder than php. If you are dead set against taking the time to learn databasing, then cookies is your only option, but any user can just delete the cookies to get around that. Quote Link to comment https://forums.phpfreaks.com/topic/166749-how-can-i-make-a-page-restricted-to-three-daily-pageviews/#findComment-880054 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.