Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Once per session

Status
Not open for further replies.

mksolutions

IS-IT--Management
Jun 17, 2004
12
US
I run a photo site written in PHP with a MSSQL backend. Everytime a photo is displayed, I add one to a "viewed" field, showing how many times that photo has been viewed. It appears someone has figured out that all they need to do to bump the count is keep hitting refresh. Is there a way to prevent them from doing that without keeping track of every photo they have viewed? I dont really want to track homy many times 1000 people view 9000 photos.

Thanks!

Matthew
 
How I would do it:

-Use cookies to keep track of who has viewed a photo. Sure, they can be cleared, but its safer than nothing.
 
Isnt there a limit on how many variables can be set in a cookie? What if the user views all 9000 photos? I like the idea of the cookie keeping track, then after say a few days the cookie will be gone. I am just not sure about how much data can be stored in a cookie.

Thanks for the response!

Matthew
 
I just checked...

"Browsers are required to retain no more than 20 cookies for a single domain"

There goes that idea. Is the only way to accomplish this to create a new table and track what user views what photo? That is a lot of database activity for not much use.

Matthew
 
Is is possible to store that much in session variables? Since it is on the server, space shouldnt be a concern. What is the limit on session variables?

Matthew
 
I just tried setting up so the photo viewed is stored in a session variable. It seems to be working fine, allowing a user to only add one view per session. I am still curious now many session variables I can set. Will a user max these out if they visit 1000 photos???

Thanks!

Matthew
 
What about just recording the hit by the current IP just once per day/hour? If the people don't see the count go up with each refresh they'll stop trying that.
 
What about just recording the hit by the current IP just once per day/hour? If the people don't see the count go up with each refresh they'll stop trying that."

Do you mean saving that data in the database?

I could easily make it save the IP and photo in the database, then having those records expire after say an hour. I am just looking at the performance side. What if, in that hour, there are 1000 different people online, each looking at 1000 photos in that hour. That means there are 1 million records that have to be searched everytime someone views that photo, to check to see if it has been viewed before. That seems like a lot of overkill, just to make sure a user does not view a photo more than once.

I am still testing the session idea. It seems to be working fine so far. I am still curious on the impact of the server with all of the session variables. What is more efficient, 1 million database entries, or 1 million session variables?
 
Who needs a million?
You would not record the photo 'hit' as the index, but the IP. There would be in your example a max of 1000 IPs, indexed numerically. First you find the calling IP. If it's new, a new record is added. If it already exists the hit markers are checked. No big deal, no performance issue.
 
I guess I do not understand how you would distinguish between the photos. Yes, IP 1.1.1.1 has visited, but you have no way to know which photos they have viewed unless you also record those. I am showing EACH photo viewed, not just each time they visit.
 
When I referred to the 'hit' markers that's what keeps track of the photos' hits.
If you create a table that has 2 columns, on for the ip2long representation of the IP (bigint), one for the photo_id (int) and a timestamp you'll end up with a table assuming 1 million records which is less than 16 MB of size. An index by ip plus photo id will find if there was a hit by the same IP within the window.
A garbage cleanup routine invoked every x% of accesses removes records older than 3600 seconds.
This really is not much different from a database driven session handler.
Admitted, there's some work, however, it's up to you to decide if it is worth doing it for keeping a more accurate count of images viewed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top