Baylock
Baylock

Reputation: 1264

PHP/MySQL: Prevent inserting according to several columns

When a user connects to a certain webpage I insert his IP address and the datetime of the connexion in MySQL.

For a reason I don't get, I often get 2 or 3 insertions for the same IP address at the exact same time.

Moreover, I don't want to add a new MySQL row if the user opens the webpage again within the 30 next minutes (I would consider this as the same visit from the user).

Would it be possible to only insert a user IF his IP address hasn't been recorded at least for a range of half an hour?

Here is the simple request I use:

INSERT INTO connex VALUES ('','".$ip."','".$datetime."')

And this is how it looks like in MySQL (id/ip/datetime):

83 | 66.249.81.57 | 2014-08-11 23:52:21
84 | 66.249.81.57 | 2014-08-11 23:52:21
85 | 66.249.81.57 | 2014-08-11 23:52:21

Thank you.

Upvotes: 1

Views: 46

Answers (1)

jvecsei
jvecsei

Reputation: 1993

Make a select statement like

SELECT ip FROM connex WHERE ip = $ip AND datetime > DATE_SUB(NOW(), INTERVAL 30 MINUTE)

And check if you get a result for this request.

Upvotes: 2

Related Questions