IP address management, also known as IPAM, is the administration of a system for tracking and managing IP addresses. This is an important task for network administrators as it helps to ensure that IP addresses are allocated efficiently and that conflicts are avoided.

In this tutorial, we will create a basic IP address management system using PHP. This system will allow us to add, edit, and delete IP addresses, as well as search for available IP addresses.

1. Create Database Table
First, we need to create a database table to store the IP addresses. Run the following SQL query to create a table named `ip_addresses`:

CREATE TABLE `ip_addresses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip_address` varchar(50) NOT NULL,
`is_reserved` tinyint(1) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Create PHP Script
Create a new PHP file named `ipam.php`. This file will contain all of the code for managing the IP addresses.

3. Database Connection
Start by establishing a connection to the database. Update the following code with your database credentials:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit(‘Database connection failed: ‘ . $e->getMessage());
}

4. List IP Addresses
Next, let’s create a function to retrieve and display a list of all the IP addresses in the database. Add the following code to your `ipam.php` file:

function listIPAddresses()
{
global $pdo;

$stmt = $pdo->query(‘SELECT * FROM ip_addresses’);
$ipAddresses = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo ‘

‘;
echo ‘

‘;

foreach ($ipAddresses as $ipAddress) {
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
}

echo ‘

ID IP Address Is Reserved Action
‘ . $ipAddress[‘id’] . ‘ ‘ . $ipAddress[‘ip_address’] . ‘ ‘ . ($ipAddress[‘is_reserved’] ? ‘Yes’ : ‘No’) . ‘ Edit | Delete

‘;
}

5. Add IP Address
Create a function to add a new IP address to the database. Add the following code to your `ipam.php` file:

function addIPAddress($ipAddress, $isReserved)
{
global $pdo;

$stmt = $pdo->prepare(‘INSERT INTO ip_addresses (ip_address, is_reserved) VALUES (?, ?)’);
$stmt->execute([$ipAddress, $isReserved]);
}

6. Edit IP Address
Create a function to update an existing IP address. Add the following code to your `ipam.php` file:

function editIPAddress($id, $ipAddress, $isReserved)
{
global $pdo;

$stmt = $pdo->prepare(‘UPDATE ip_addresses SET ip_address = ?, is_reserved = ? WHERE id = ?’);
$stmt->execute([$ipAddress, $isReserved, $id]);
}

7. Delete IP Address
Create a function to delete an IP address from the database. Add the following code to your `ipam.php` file:

function deleteIPAddress($id)
{
global $pdo;

$stmt = $pdo->prepare(‘DELETE FROM ip_addresses WHERE id = ?’);
$stmt->execute([$id]);
}

8. Display IP Address Form
Create a function to display a form for adding or editing an IP address. Add the following code to your `ipam.php` file:

function displayIPAddressForm($id = null)
{
global $pdo;

if ($id) {
$stmt = $pdo->prepare(‘SELECT * FROM ip_addresses WHERE id = ?’);
$stmt->execute([$id]);
$ipAddress = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
$ipAddress = [];
}

echo ‘

‘;
echo ‘‘;
echo ‘IP Address:
‘;
echo ‘Is Reserved:
‘;
echo ‘‘;
echo ‘

‘;
}

9. Handle Form Submission
Finally, add code to handle form submissions. Add the following code to your `ipam.php` file:

if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
$ipAddress = $_POST[‘ip_address’];
$isReserved = isset($_POST[‘is_reserved’]) ? 1 : 0;

if ($_POST[‘id’]) {
$id = $_POST[‘id’];
editIPAddress($id, $ipAddress, $isReserved);
} else {
addIPAddress($ipAddress, $isReserved);
}
}

10. Output
To display the list of IP addresses, add the following code to your `ipam.php` file:

listIPAddresses();

To display the IP address form, add the following code to your `ipam.php` file:

displayIPAddressForm();

11. Testing
Save the `ipam.php` file and open it in your browser. You should see a table listing all of the IP addresses in the database, as well as a form for adding new IP addresses at the bottom of the page.

You can add, edit, and delete IP addresses using the form and the links provided in the table.

That’s it! You have now created a basic IP address management system with PHP. You can expand on this system by adding additional functionality, such as searching for available IP addresses or implementing user authentication.