Current Location: Home> Latest Articles> Complete Tutorial and Code Examples for Implementing a Friend Links Feature in PHP

Complete Tutorial and Code Examples for Implementing a Friend Links Feature in PHP

M66 2025-09-17

Concept of Implementing Friend Links in PHP

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.

Create the Database Table

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:

  • id: Auto-increment primary key, unique for each link.
  • title: Link title.
  • url: Link address.
  • status: Link status, 1 for active, 0 for disabled.
  • created_at: Creation time.
  • updated_at: Update time.

Create the Friend Links Display Page

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.

Create the Friend Links Management 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.

Running and Testing

Once the files are created:

  • Visit links.php to view the friend links list.
  • Visit admin.php to manage links and add new ones.

Make sure to replace localhost, your_database, username, and password in the sample code with your actual database credentials.

Conclusion

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.