Good Morning.

I am writing a little function to record the visits to a landing page, and want to do it in such a way as to not count multiple visits from the same IP address within a 24 hour period.

What I was wondering is if there is a way to do a conditional insert based on the IP address not having a matching record with the visit_date less than 24 hours old. So it would cover whether there was a record with that IP or not.

Here is the basic structure I think will work, but I need some direction in the area of the " now ( ) - 24 hour " part... I know that syntax isn't correct, but just put it there to show my thought process.

any assistance would be greatly appreciated.

 $sql_i = "
  INSERT INTO lead_visit(visit_id, mem_id, page_name, visit_date, visit_ip)
  VALUES('', '$mem_id', '$page_name', now(), '$visitor_ip')
  WHERE ((select count(*) from lead_visit where visit_date>(now()-24 hours)) = 0)

Recommended Answers

I agree with logging everything. That's also a fine way to notice abuse. If you don't log it, you won't know.

Jump to Post

All 4 Replies

While I would still like some feedback on that question just for educational purposes, I wonder if it wouldn't be better to just record ALL visits even if from the same IP, and then sort that out on the reporting end.

That way, I could show the 'RAW' visits and the unique visits within the past 24 hours / past week / past month / past year.

I'm thinking that would probably be much easier to manage and actually give more complete information.

I agree with logging everything. That's also a fine way to notice abuse. If you don't log it, you won't know.

I think it's much easier to replace the where part with where date_format(date, '%Y/%m/%e') <> CURRENT_DATE
Basically strip the time from datetime.

Same goes if you want to select 1 visit per IP per day, use the date_format with a distinct and you are done.

Thank you for the replies.

pritaeas, you are correct. I opted to take that route, and it is working fine for the data collection. Now just need to create the page so the members can see the results of their pages, and it should all be fine... Thanks

adam_k - That approach would work to a degree, but wouldn't truly be 24 hours since I could visit the page at 11:30 pm and then at 1 am the next day, only 1 1/2 hours apart and it would be counted as 2 visits.

I think I will be able to utilize that in another way... thanks again for your response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.