I'm attempting to create a referral system on my website which combines a user's IP address, username, and user agent all combined and then hashed to md5 to create a unique ID (shortened to 12 characters) for every user. The plan is to use a MySQL database to keep track of how many referrals (unique clicks on their referral link) each user has without using cookies or account creation. I have no formal education in PHP or MySQL so I've been trying to piece this together through searching online but I've found myself stuck in a tough spot.
I have successfully created a unique 12 character identifier for each user, and I've figured out how to pull the 12 character identifier from the referrer-URL. I can't figure out how to record and organize the user's unique referral id and referral amount while also crediting the referrer with 1 referral per unique ID (starting at 0). any advice would be much appreciated! thank you.
<!DOCTYPE html>
<html>
<body>
<?php
$ip = $_SERVER["REMOTE_ADDR"];
$user = $_SERVER["REMOTE_USER"];
$agent = $_SERVER["HTTP_USER_AGENT"];
$unique_id = substr(md5($ip . $user . $agent), 0, 12);
$full_ref_uri = $_SERVER["REQUEST_URI"];
$ref_uri = ltrim($full_ref_uri, "/?");
$servername = "myserver.com";
$username = "myusername";
$password = "mypassword";
$dbname = "mydb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//this displays the referrer unique_id
echo $ref_uri;
echo "<br>";
//this records the user's unique id in the sql database
$sql = "INSERT INTO id (unique_id)
VALUES ('$unique_id')";
//this displays the user's generated referral link using their unique_id
if ($conn->query($sql) === true) {
echo "http://my.website.com/?";
echo $unique_id;
} else {
echo "http://my.website.com/?";
echo $unique_id;
echo " already exists.";
}
$conn->close();
?>
</body>
</html>