Current location: Hot Scripts Forums » General Community » Script Requests » Open page once per day per IP


Open page once per day per IP

Reply
  #1 (permalink)  
Old 09-06-09, 01:24 AM
maxco61 maxco61 is offline
New Member
 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rolleyes Open page once per day per IP

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
Reply With Quote
  #2 (permalink)  
Old 09-09-09, 03:09 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Catches in tracking by IP/Cookie

You can do this in either Javascript or PHP.

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.
Reply With Quote
  #3 (permalink)  
Old 09-09-09, 08:35 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
PHP Code:

//Set a cookie with a 24 hour exp date such as this:

$one_day 60 60 24 time(); 
setcookie('smitten'"yes"$one_day); 

//Then use a code like this to test if it is set:

if(isset($_COOKIE['lastVisit'])){header('Location: REDIRECT_HERE);}else{} 

Sorry I dont know the tags to make it highlight.

Last edited by Nico; 09-10-09 at 10:13 AM.
Reply With Quote
  #4 (permalink)  
Old 09-10-09, 10:14 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by ruteckycs View Post
Sorry I dont know the tags to make it highlight.
http://www.hotscripts.com/forums/misc.php?do=bbcode#php

Reply With Quote
  #5 (permalink)  
Old 09-10-09, 11:00 AM
unstabledreamer unstabledreamer is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
hey what is a fourm prefix? and why when i post does it say i dont have one?
Reply With Quote
  #6 (permalink)  
Old 09-10-09, 07:38 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
Thanks Nico by the way there is an error in my code

This line:
PHP Code:

if(isset($_COOKIE['lastVisit'])){header('Location: REDIRECT_HERE);}else{} 

Should be

PHP Code:

if(isset($_COOKIE['smitten'])){header('Location: REDIRECT_HERE);}else{} 

Reply With Quote
  #7 (permalink)  
Old 09-12-09, 06:05 AM
LooieENG LooieENG is offline
New Member
 
Join Date: Sep 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Create a database with this structure

table name: logs
column 1 name: ip
column 1 type: varchar(15)
column 2 name: time
column 2 type: int

and on the page you want to protect, use this

PHP Code:

<?php


mysql_connect
('localhost''MYSQLUSERNAME''MYSQLPASSWORD');
mysql_select_db('MYSQLDATABASENAME');

mysql_query('DELETE FROM `logs` WHERE ' . (time() - 86400) . ' >= `time`');

if (
filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP)) {
        
$result mysql_query('SELECT `time` FROM `logs` WHERE `ip` = "' $_SERVER['REMOTE_ADDR'] . '"');
        if (
mysql_num_rows($result) === 0) {
                
mysql_query('INSERT INTO `logs` VALUES ("' $_SERVER['REMOTE_ADDR'] . '", ' time() . ')');

?>

<!-- display your html code here. if you need to use PHP, do it like this -->

<div id="blah">blah, this is some html but wait, I need <?php echo 'PHP!' ?>!</div>

<?php

        
} else {
                exit(
'Sorry, you have already visited this page in the last 24 hours!');
        }
} else {
        exit;
}

?>
That should work
Reply With Quote
  #8 (permalink)  
Old 09-12-09, 07:16 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Remember, in a densely populated area, you may have many different visitors coming through the same IP address.
Reply With Quote
  #9 (permalink)  
Old 09-13-09, 01:49 AM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
Or different visitors on the same IP via a LAN, That is why I suggest the cookie method. Restrict them by computer.
Reply With Quote
  #10 (permalink)  
Old 09-14-09, 05:01 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Working Javascript method (cookie based)

Code:
  <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>

Borrowed cookie code from JavaScript - Cookies

Last edited by carters-site; 09-14-09 at 05:03 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
open word file in a specific asp.net page mariowowo ASP.NET 1 06-28-08 01:47 AM
External link to open in same page sparkiii Script Requests 7 03-27-07 03:15 AM
depending on the day is the page displayed pkcidstudio Script Requests 7 08-22-06 05:05 PM
Page reload (Another is this possible) chrisrobertson HTML/XHTML/XML 0 07-31-06 07:30 PM
Open feed back page pallabmondal123 JavaScript 2 03-06-06 02:38 PM


All times are GMT -5. The time now is 09:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.