Pauls74462 Posted June 26, 2009 Share Posted June 26, 2009 I have the following code to collect information in the db from visitors. <?PHP include ("config.php");// Make a MySQL Connection$link2 = mysql_connect( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());if (!$link2) { die('Could not connect: ' . mysql_error()); }// echo "Connected to db ";// select the databasemysql_select_db($dbname) or die ("Could not select database because ".mysql_error());// create table on database$ip=$_SERVER['REMOTE_ADDR']; $ref=@$HTTP_REFERER; $date = date("l F, dS Y @ h:i A");$query = "INSERT INTO view (view_ip, download, rep, date, ref)VALUES('$ip', '$download', '$MyRunCode', '$date', '$ref')";mysql_query($query) or die(mysql_error());mysql_close();?> What I like is for it to only collect information from the user 1 time every 24 hrs.Any suggestions?PaulPS:I have the ideal of comparing the IP address with the IP address on the db and if today's date is with that IP address then do not save to the db. But how? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 26, 2009 Share Posted June 26, 2009 You can use a SELECT statement to get the date corresponding to a certain IP, if it exists, and compare it with the date you're saving.SELECT `date` FROM `view` WHERE `view_ip` = 'xxx.xxx.xxx.xxx' Link to comment Share on other sites More sharing options...
Pauls74462 Posted June 29, 2009 Author Share Posted June 29, 2009 You can use a SELECT statement to get the date corresponding to a certain IP, if it exists, and compare it with the date you're saving.SELECT `date` FROM `view` WHERE `view_ip` = 'xxx.xxx.xxx.xxx'Can you explane more on how to place this code in the above code? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 29, 2009 Share Posted June 29, 2009 Once you have the IP you can create a query like above, and use mysql_query to send it to the server. It will give you a result set back that will have 0 or more records. It looks like you're storing your date as a string, it would be ideal to store it as a timestamp or something else that can be sorted like a date. If you can't sort the database results by date, then you'll need to loop through everything in the result set and compare each one to see if it's within 24 hours. You would use a while loop to loop through the result set: $result = mysql_query($sql);while ($row = mysql_fetch_assoc($result)){ echo $row['date'];} For each row in the result set, you would need to get the date and compare it. In order to turn your date string into a date that PHP can use, you might be able to use the strtotime function:http://www.php.net/manual/en/function.strtotime.phpstrtotime will convert most date strings into a Unix timestamp, and you can get the current Unix timestamp using the time function:http://www.php.net/manual/en/function.time.phpThe timestamp is the number of seconds since 1/1/70, since there are 86400 seconds in a day you would check to see if the two timestamps are within 86400 seconds of eachother. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.