Jump to content

How can I make a page restricted to three daily pageviews?


jdock1

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

<?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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.