problem assigning gift to user

Please help a complete noob. :slight_smile:

I have the following problem:
User1 enters site the first time and is given gift1 at second visit gift2 and so on…
User2 enters site the first time and is given gift1 at second visit gift2 and so on…
Each user can receive each gift only 1 time.

Users are identified by IP

Db structure:
table IP_used (id, ip_address)
table gift (giftid,description)

So far I’ve managed to do this: user enters site > check in db if ip exists > if it doesn’t exist add and display 1 > else display 2

[php]<?php

$db = new PDO(
‘mysql:host=localhost;dbname=**********’,
’,
'

);

$ipData = [ ‘:ip’ => $_SERVER[‘REMOTE_ADDR’] ];

$statement = $db->prepare(’
SELECT count(*) FROM IP_used
WHERE ip_address = :ip
');

$statement->execute($ipData);

if ($statement->fetchColumn() == 0 ) {
// add to list
$statement = $db->prepare(’
INSERT INTO IP_used (
ip_address
) VALUES (
:ip
)
');
$statement->execute($ipData);
// do whatever it is you are doing for first-visit
echo(1);

} else {
// subsequent visit content here
echo(2);
}

?>[/php]

Just run a INSERT query adding the correct data to the user. I’d suggest adding another table that links the user and the gift table, ie
user_gift
user_id, gift_id

[hr]

Note that you shouldn’t really rely on the IP address to identify users. They usually aren’t unique to a single user, and they’re even likely to change between visits.

[hr]

ps: Great that you’re using PDO with prepared/parameterized queries.

Thank you, let me see where i can get from here.
In this case IP’s are static assigned on the intranet so it’s ok. It wouldn’t work on the internet though.

It still is not as unique identifier as would be useful. Using a user id or some other values that links to a specific user would work more efficiently.

Sponsor our Newsletter | Privacy Policy | Terms of Service