Limiting page visit for same IP with php - php

I have a page, i want so that 1 IP could use the page for lets say 10 times a day at most. 1 refresh = 1 use. After some research i read that this can be done with javascript or php but with cookies. Couldnt the user just delete his cookies and use the page again?
Is there a way to do it only with php and no cookies? Any suggestions?
Note: i am not using any databases.

It can't be done without a database. You simply need to keep track of all IPs and you can't do that without a database.
Of course, a simple text file can also go for a database. For every visitor that accesses a certain page, log that IP address to a text file. To see whether they can access the page, just count how many times the IP address is in the file.

You can create a database table and then on each view either insert the IP (if its not already in the database) or increment views by 1. If views = 10 don't allow the user to visit.

You can use htaccess file too. But you could write ipaddress/nrofrefreshes records to a text file located on the server.

Related

The most efficient way to handle visitor/user statistics that I want to catch and display?

I am building a link directory style web application. For simplicity all of the following are examples. On my website I have 10 categories. Each category has it's own page and each page has 100 links in a table format. Each link has many columns like name, id, url, etc but the focus of this question deals with the "time last viewed" column. It will display a default text if the user/visitor has never clicked the link however if the link has been clicked by the user prior to the visit it will display the time/date the user last visited that link.
The way I have it set up is when the user clicks the link they are sent to another page/script (using GET method. link 1 is appended with ?rid=1) I use a switch contruct. (Case value is 1 from $_GET execute code block) this code block is where i need the user statistics caputuring to happen. Once the function runs and both captures and stores the visit statistics info the user is sent to the requested resource via header location. So the next time to user sees the list of links on the category page the link they visited will now display the time they visited it.
On my production site i have up to 1000 links. If they clicked each link it would say next to each link the last time they clicked it. Important to include users will be logged in when clicking each link.
How would you go about doing this? Store the info in a cookie or in the database? As there are 1000 links there could be 1000 different values. Thanks in advance.
It isn't a lot of data so you can do both, store in the database as well as store in a cookie. Ideally for performance, you should retrieve from the cookie first and then retrieve from the database if the cookie doesn't contain any user information pertaining to that link. Depending on your performance requirements and the amount of traffic you anticipate, you can use database storage, in-memory storage and asynchronous updates.
database updates are instant but can impact overall performance and page load times
in-memory caching such as apc gives best performance but data needs to be synchronised to the database
asynchronous updates are great for balancing out performance hits because you can register a view from the client side using JavaScript after the page has loaded, rather than during php execution on server side.
Personally I would use all 3 if possible because it gives a good platform for future development.

How to limit a user to one visit on a single page?

Hello Guys i hate asking stupid questions here so i hope this isn't, How would i go about limiting someone to a download page of mine? so if they try to visit that page again (more then once) to download something it will just redirect or preferably change the download links to link 2 then Link 3 and is it possible to do without a database?
Eg:
First Visit - main link
Second Visit- link 2
third Visit- link 3
4 and up Visit no link and redirect
Maybe with cookies? i really i have no idea how to do it and i have Googled it but my wording must not be there...
Is there a name for this or a script?
Thanks for your time Guys.
A.
The best method to achieve the desired goal is database. Create a database table that contains two columns :
(1)Page Visitors IP
(2)The Last Download link used by the visitor to download
file(contents) from your website.
B.
You can too achieve your goal with the help of COOKIE.
setcookie("Visitor IP", "Download Link used by the Visitor", $expire);
Everytime, visitors visit your website, fetch the visitor IP and check whether $_COOKIE["Visitor IP"] is set or not, if its set, then update the existing Cookie else create the new one.
However, using Cookie is not a convenient way, as there might be a case where
Browser does NOT Support Cookies.
Client alter the Cookies value and use the previous link for download.
So, most simplest and elegant way to do it is, using Database.
UPD:
*How easy is it to code/setup a database?*
Setting up/Connecting to a database in php is pretty easy.
Refer the following LINK
Coding is pretty easy as well.
-Whenever the visitor click on the download link, fetch Visitors IP ($fetched_IP) by either POST or GET method. Also fetch the Link ($URL) visitor has clicked.
-Query the database [eg: Select DB_IP,LASTLINK from database WHERE DB_IP=$fetched_IP.....]
-If RowCount>0, then IP($fetched_IP) exists in database. Check the Last Link visited by the $fetched_IP.
-If LASTLINK!=$URL, then allow him download the content from $URL.Update the LAST_LINK column in database table by $URL.
-If rowcount==0,(New User) Insert a row that contains DB_IP=$fetched_IP(Visitor IP) and LAST_LINK=$URL.
An easy was is to use a hash table (associative array). When they satisfy the criteria to access the file, add an entry to the hash table using the the unique url as the key and the document path as the value. Save it to the session. When they access the url the page checks to see if the url is in the hash table. If it is, remove the url from the hash table and stream the file. If you wanted to allow multiple uses, you could store a countdown variable along with the url, that will decrement with every access and only delete the url from the hash table when the count is zero.

modify PHP file to limit user download per month to 5 TIME ONLY

On my web site, I have a user account home page. Users can create apps (like menus
and functions).
After the user finishes, he can click on a link leading to a page that allows him to download the source code for what he created.
What I need is to limit him from visiting the download page more than 5 times every month. After 5 times he shouldn't be able to download the source code until the next month.
You can see the download.php page that I want to limit here: download.php
You can achieve what you want by storing the files you want to protect outside the web root, and then using readfile() to send it to the user via PHP when they try to download (and if your code permits it).
So you'll need to add a database to count the number of downloads for a particular user, and of course a mechanism to detect which user is doing the download. This is usually accomplished using session cookies.
Edit: You'll need a new table that counts the downloads per user, so create that. Also you'll need a table of downloads, so you can display the list to the user, so create that. Then write some code to render that list to the screen wherever you want that information, and then link each one to my_download.php?file=xxx where xxx is your primary key for that table. When this is accessed, ensure that your user is logged in, check that they've not exceeded their download limit, and if all is good, allow them to download using the above approach.

Tracking internal link php

I want to set up a few internal statistics for one of my dynamic sites. The idea is to make available to each member of the site:
a) How many times the profile has been seen in the day (1 click = 1 ip = 1 view)
b) How many times the profile has been seen in the month (1 click = 1 ip = 1 view)
c) How many have left since the mail button "contact".
Before developing this in php, I wanted to know if you would not have a resource that these actions. It would save me some time.
Sincerely,
Well, you would just simply need to have a DB where you could save those statistics. Then, you would create a class with a few functions that save statistics to this DB. E.g.
function addPageview($pageIdentifier, $loggedInUser) {
// code to save to DB
}
Then, when a page is viewed (e.g. the profile page of someone), you do a call to this addPageview() with the correct page identifier (e.g. the URL) and the logged in User so you know who has viewed the page. You leave $user empty if there is no logged in user.
Good luck!
So if you want to increase your profile-views counter by 1, you can restrict this to do so every 24 hours by setting a cookie on the visitors computer with that specific users ID. The user can clear their cookies and visit the profile again, but "commoners" dont know about this technique.
In your code for viewing the profile, you use the following pseudocode:
if user has no cookie
bump views up by 1
So I create my own internal link tracker for ZF.
I don't use cookie.
I check if an ip is already back on the site. If so, I change the date of last visit, otherwise I created. Then, I check if the called page has already been visited. If so, I change, otherwise I insert. Then, I check if the association ip / page exists: if so, I change, otherwise I insert.
In the end, I can have a system of click per day, month, year, and for su ...
I wrote a tutorial on the occasion on my blog, because now it is only really suited to the current project.
Thank you for your support.

how to handle online status. guest, users etc

Whats the best way to keep track of how many users and guests are online? Im making a forum for fun and learning
Right Now I have a 2 fields in the users table called is_online and last_access_time.
If current time is 5 minutes or more than last_access_time i set is_online it to zero. And if the signed in user refreshes browser i set it to 1.
But what about guests? I wanna keep track on how many guests are on also
Another thing that would be really cool is to show what page the user is viewing. and on the page, forum thread for example, 5 guests, Homer and Shomer are viewing this page. But how should i structure this? Hmm maybe i should make another question for that.
i dont know what i should do
What do you suggest?
I'd use cookies for this. Set a cookie when the user enters (checking first to make sure one doesnt exist). Easy way to generate a unique id for that user is to hash their IP plus the current time.
$id = md5($_SERVER['REMOTE_ADDR'] . time());
Store that id in your database and use that to reference
You can check what page they are viewing by grabbing either $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] near the top of your php source. Store that in the table. I'd take a look at php.net's explanation of whats stored in the _SERVER global, as it should help out quite a bit if you find that you need more then just the document they are on (ex index.php). Found here.
You may need to pull apart of the query string that was used to access that page, parse out the variables to determine the page they are requesting. Either way, this could all be done through cookies, or just use a single cookie to store the unique id and use your table for storing everything else.
You cannot know for certain which page a user is viewing, but you can keep track of which page they last viewed. Every time you deliver a page to a user, record that page's path in a database row associated with them. Voila.
To keep the number of guests, I suggest tracking the number of distinct unauthenticated IP/HTTP-User-Agent combinations seen on a certain page in the last X minutes.
I found this article on Web Monkey that might help you.
http://www.webmonkey.com/2010/02/how_many_users_are_on_your_site_right_now/

Categories