Friend links are a common feature on websites that help build connections with other sites, improving interactivity and driving traffic. Implementing this feature in PHP is not complicated — it mainly requires designing a proper database schema and writing the related code. Below is a step-by-step guide with examples.
First, create a database table to store friend link information. Example SQL:
CREATE TABLE `links` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `url` varchar(255) NOT NULL, `status` tinyint(1) NOT NULL DEFAULT '1', `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Main fields:
Next, create a page to display the list of links, for example links.php:
<?php // Connect to the database $pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password'); // Query all active links $stmt = $pdo->prepare('SELECT * FROM links WHERE status = 1'); $stmt->execute(); $links = $stmt->fetchAll(PDO::FETCH_ASSOC); // Display the list of links foreach ($links as $link) { echo '<a href="' . $link['url'] . '">' . $link['title'] . '</a><br>'; } ?>
This code retrieves all active links from the database and outputs them to the page.
To make it easier for administrators to manage friend links, create an admin page, e.g., admin.php:
<?php // Connect to the database $pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password'); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = $_POST['title']; $url = $_POST['url']; // Insert new link into database $stmt = $pdo->prepare('INSERT INTO links (title, url, created_at, updated_at) VALUES (?, ?, NOW(), NOW())'); $stmt->execute([$title, $url]); echo 'Link added successfully!'; } ?> <form method="POST" action="admin.php"> <input type="text" name="title" placeholder="Link Title" required><br> <input type="url" name="url" placeholder="Link URL" required><br> <input type="submit" value="Add Link"> </form>
This page allows administrators to add new links via a form, which are then stored in the database.
Once the files are created:
Make sure to replace localhost, your_database, username, and password in the sample code with your actual database credentials.
This article demonstrated how to implement a friend links feature in PHP. By designing a database table, creating a display page, and building an admin management interface, you can easily set up a complete friend links module. This feature not only facilitates inter-site connections but also enhances user experience and SEO performance.