Hello everyone,
I need a script ( PHP or JAVASCRIPT) that allow a page to be viewed ONCE PER DAY basing on visitor's IP address. Any help ?
Many thanks in advance.
Massimo
JavaScript alone would have to rely on the use of a cookie with a 24 hour expiration date. the issue with this is all the user would have to do is clear their cookie and they would be able to view the page.
Using PHP you would have to store the IP address in someway whether it be a flat file or in a database. The latter being the preferred method for quicker retrieval. The catch to limiting a site
by IP is that there are computers that are networked that would externally appear to have the same IP. This would mean not just one persons computer gets locked out but the entire company gets locked out. For this reason it can be problematic to rely on IP.
This is one reason forcing a user to register is preferred that way you actually can determine who is viewing the site and lock it out by user. Still if they setup multiple user accounts they can bypass this. I am not sure of the best complete way to do what you are asking but those are the options and the pitfalls of each method.
<script language ='javascript'>
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
/* Check the cookie and redirect if they have it set. */
sWhere = 'http://google.com'; // replace this with where you want them to be sent when they can't view page.
if(!readCookie('viewcheck')){
createCookie('viewcheck',1,1);
} else {
window.location = sWhere;
}
</script>