Forum Moderators: coopster

Message Too Old, No Replies

Best way to limit unique page views

         

trigoon

2:00 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



I'm working on building a script (specifically a gallery type script) in which I would like to incorporate a system where guests are limited to a certain set number of image views before they are prompted to login.

My question is, what would be the best way to limit the number of unique image views? In a previous script I made I made use of sessions, and built an array containing the image ids of the images they have viewed. I logged in a database how many views the IP had made.

However as you can imagine using the IP method isnt always best.

So I come to you :) What are some alternatives you can suggest which would be effective but most importantly FAST.

Thank you in advance.

punisa

2:37 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Hmm.. tricky stuff.
IP method will work for short time, until they re-connect. Otherwise if they use a proxy its useless really. I'm sure you already know that.
Cookies? Nah they can just easily clean them up..

What about SwfObjects? As I recall these are sort of flash cookies, do they get deleted as well? I'm not sure.
Would be worth to investigate.

d40sithui

2:41 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



I think the sole use of sessions would be just fine. You don't want it too be so strict that the user gets annoyed and don't want to see your site anymore. After storing several counters in a session variable, prompt them to login - otherwise they have to close the browser and load it again.

trigoon

2:42 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Yeah, at the moment I'm not too worried about them re-connecting and changing their IP. The IP method in that case would work however I'm worried about the load on the server when it has to update and check the database everytime an image is viewed, which when I have a few people on the site wouldn't be a problem but when (as I'm expecting through previous experience) I have a couple hundred or more at once may cause issues. So right now performance is my main issue.

trigoon

2:44 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Thanks d40, the thing is that although I don't want to be overly strict I also don't want it to be as simple as destroying their session to bypass the limits.

Perhaps the mysql method is the best way to do for that however can some suggest a proper database structure and efficient queries I could use to prevent overloading the server at high traffic times.

rocknbil

4:08 pm on Apr 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My question is, what would be the best way to limit the number of unique image views?

Since you're in the PHP forum, my guess is this is how you would prefer to do it.

- Write a little script that outputs images. Using PHP open methods. Assume the image and user is registered in the database, and the image id is 123.

on request of "img_id=123"

- check views for user for img id 123, if exceeded, send to log in.
- otherwise open file with PHP
- print appropriate content-type header (image/gif/image/jpg, etc.)
- while reading in file, print to browser
- close file (don't forget to do this, flushes buffer)
- increment user's view in the database.

Change all your image src's to

<img src="your_image_script.php?img_id=123">

Done. Well, almost. Note that this doesn't manage unique views and will increment refreshes, so you'd need session variables or cookies to make sure it's a unique request. Should be elementary from there, though.