**MySQL Database Schema**
sql
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
referral_code VARCHAR(255) NOT NULL,
position INT NOT NULL DEFAULT 1,
referred_by INT NULL,
PRIMARY KEY (id),
FOREIGN KEY (referred_by) REFERENCES users(id)
);
```
**PHP Code**
**index.php**
```php
<?php
session_start();
// Connect to the database
$mysqli = new mysqli("localhost", "root", "password", "tiktok_follow_system");
// Get the referral code from the URL parameter
$referral_code = isset($_GET['ref']) ? $_GET['ref'] : null;
// Check if the referral code is valid
if ($referral_code) {
$sql = "SELECT * FROM users WHERE referral_code = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $referral_code);
$stmt->execute();
$result = $stmt->get_result();
$referrer = $result->fetch_assoc();
if (!$referrer) {
// Invalid referral code
header("Location: index.php?error=Invalid referral code");
exit;
}
}
// Check if the user is already logged in
if (isset($_SESSION['user_id'])) {
// Redirect to the user's profile page
header("Location: profile.php");
exit;
}
// Display the sign up form
?>
<!DOCTYPE html>
<html>
<head>
<title>TikTok Follow System</title>
</head>
<body>
<h1>TikTok Follow System</h1>
<form action="signup.php" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required>
<label for="referral_code">Referral Code:</label>
<input type="text" name="referral_code" id="referral_code" value="<?php echo $referral_code; ?>">
<input type="submit" value="Sign Up">
</form>
</body>
</html>
```
**signup.php**
```php
<?php
session_start();
// Connect to the database
$mysqli = new mysqli("localhost", "root", "password", "tiktok_follow_system");
// Get the form data
$username = $_POST['username'];
$referral_code = $_POST['referral_code'];
// Check if the username is already taken
$sql = "SELECT * FROM users WHERE username = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$existing_user = $result->fetch_assoc();
if ($existing_user) {
// Username already taken
header("Location: index.php?error=Username already taken");
exit;
}
// Check if the referral code is valid
if ($referral_code) {
$sql = "SELECT * FROM users WHERE referral_code = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $referral_code);
$stmt->execute();
$result = $stmt->get_result();
$referrer = $result->fetch_assoc();
if (!$referrer) {
// Invalid referral code
header("Location: index.php?error=Invalid referral code");
exit;
}
}
// Create a new user
$sql = "INSERT INTO users (username, referral_code, referred_by) VALUES (?, ?, ?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("ssi", $username, $referral_code, $referrer['id']);
$stmt->execute();
// Get the new user's ID
$user_id = $mysqli->insert_id;
// Store the user's ID in the session
$_SESSION['user_id'] = $user_id;
// Redirect to the user's profile page
header("Location: profile.php");
exit;
```
**profile.php**
```php
<?php
session_start();
// Connect to the database
$mysqli = new mysqli("localhost", "root", "password", "tiktok_follow_system");
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
// Redirect to the login page
header("Location: index.php");
exit;
}
// Get the user's data
$user_id = $_SESSION['user_id'];
$sql = "SELECT * FROM users WHERE id = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
// Get the user's position in the pyramid
$sql = "SELECT COUNT(*) AS position FROM users WHERE referred_by = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$position = $result->fetch_assoc()['position'] + 1;
// Display the user's profile page
?>
<!DOCTYPE html>
<html>
<head>
<title>TikTok Follow System</title>
</head>
<body>
<h1>TikTok Follow System</h1>
<h2>Profile</h2>
<p>Username: <?php echo $user['username']; ?></p>
<p>Referral Code: <?php echo $user['referral_code']; ?></p>
<p>Position: <?php echo $position; ?></p>
<h3>Your Referral Link</h3>
<p><a href="index.php?ref=<?php echo $user['referral_code']; ?>">https://example.com/index.php?ref=<?php echo $user['referral_code']; ?></a></p>
</body>
</html>
```
**install.php**
```php
<?php
// Connect to the database
$mysqli = new mysqli("localhost", "root", "password");
// Create the database
$sql = "CREATE DATABASE IF NOT EXISTS tiktok_follow_system";
$mysqli->query($sql);
// Select the database
$mysqli->select_db("tiktok_follow_system");
// Create the users table
$sql = "CREATE TABLE IF NOT EXISTS users (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
referral_code VARCHAR(255) NOT NULL,
position INT NOT NULL DEFAULT 1,
referred_by INT NULL,
PRIMARY KEY (id),
FOREIGN KEY (referred_by) REFERENCES users(id)
)";
$mysqli->query($sql);
// Create the index on the referral_code column
$sql = "CREATE INDEX IF NOT EXISTS idx_referral_code ON users (referral_code)";
$mysqli->query($sql);
// Close the connection
$mysqli->close();
?>